[Haskell-cafe] When using Cereal, what is the right way to write `get` for multi-constructor type?

Rahul Muttineni rahulmutt at gmail.com
Wed Apr 27 03:32:17 UTC 2016

Lennart Augustsson gave a nice example of how to define enums without
having to maintain the forward as well as reverse transformations here [1].
Applying that to your problem:

> instance Enum STH where
>    fromEnum = fromJust . flip lookup table
>    toEnum = fromJust . flip lookup (map swap table)
> table = [(A, 1), (B, 66), (C, 111)]

And then you can use the code I gave in the previous reply to generate the
Serialize instance. I suppose this is better looking, but you may want to
check the efficiency since it requires a lookup (but it's a super tiny
table so it might not be all that bad). You can generate similar code with
Template Haskell, eliding the lookup.


On Tue, Apr 26, 2016 at 1:40 PM, Magicloud Magiclouds <
magicloud.magiclouds at gmail.com> wrote:

> OK. So my understanding is there is no better (good-looking) code. Thank
> you all.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Rahul Muttineni
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160427/34a29494/attachment.html>

More information about the Haskell-Cafe mailing list