[Haskell-cafe] When using Cereal, what is the right way to write `get` for multi-constructor type?
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 .
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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe