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

Hi, Say I have code already like: data STH = A | B | C instance Serialize STH where put A = putWord8 1 put B = putWord8 66 put C = putWord8 111 Then what is the better way to do `get`? Is following the only one? get = do i <- getWord8 case i of 1 -> return A 66 -> return B 111 -> return C Thanks.

On 26 April 2016 at 15:39, Magicloud Magiclouds
Hi, Say I have code already like: data STH = A | B | C instance Serialize STH where put A = putWord8 1 put B = putWord8 66 put C = putWord8 111
Then what is the better way to do `get`? Is following the only one? get = do i <- getWord8 case i of 1 -> return A 66 -> return B 111 -> return C
I think that covers it, though having an explicit error message for any other value of `i` will probably be better than the default unmatched case one.
Thanks.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com http://IvanMiljenovic.wordpress.com

You may also want to use the Lambda-case extension. It will allow you to elide the declaration of i. --Will Yager On Tue, Apr 26, 2016 at 1:24 AM, Ivan Lazar Miljenovic < ivan.miljenovic@gmail.com> wrote:
On 26 April 2016 at 15:39, Magicloud Magiclouds
wrote: Hi, Say I have code already like: data STH = A | B | C instance Serialize STH where put A = putWord8 1 put B = putWord8 66 put C = putWord8 111
Then what is the better way to do `get`? Is following the only one? get = do i <- getWord8 case i of 1 -> return A 66 -> return B 111 -> return C
I think that covers it, though having an explicit error message for any other value of `i` will probably be better than the default unmatched case one.
Thanks.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com http://IvanMiljenovic.wordpress.com _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

OK. So my understanding is there is no better (good-looking) code. Thank
you all.
William Yager
You may also want to use the Lambda-case extension. It will allow you to elide the declaration of i.
--Will Yager
On Tue, Apr 26, 2016 at 1:24 AM, Ivan Lazar Miljenovic < ivan.miljenovic@gmail.com> wrote:
On 26 April 2016 at 15:39, Magicloud Magiclouds
wrote: Hi, Say I have code already like: data STH = A | B | C instance Serialize STH where put A = putWord8 1 put B = putWord8 66 put C = putWord8 111
Then what is the better way to do `get`? Is following the only one? get = do i <- getWord8 case i of 1 -> return A 66 -> return B 111 -> return C
I think that covers it, though having an explicit error message for any other value of `i` will probably be better than the default unmatched case one.
Thanks.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com http://IvanMiljenovic.wordpress.com _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

On 26 April 2016 at 18:10, Magicloud Magiclouds
OK. So my understanding is there is no better (good-looking) code. Thank you all.
The only other thing I can think of is that if you use the DeriveGeneric extension to derive an instance of Generic for your class, then you can do just "instance Serialize STH" and the get and put instances will be defined for you. This doesn't work if you want custom/specified values for your constructors though.
William Yager
于2016年4月26日周二 下午4:03写道: You may also want to use the Lambda-case extension. It will allow you to elide the declaration of i.
--Will Yager
On Tue, Apr 26, 2016 at 1:24 AM, Ivan Lazar Miljenovic
wrote: On 26 April 2016 at 15:39, Magicloud Magiclouds
wrote: Hi, Say I have code already like: data STH = A | B | C instance Serialize STH where put A = putWord8 1 put B = putWord8 66 put C = putWord8 111
Then what is the better way to do `get`? Is following the only one? get = do i <- getWord8 case i of 1 -> return A 66 -> return B 111 -> return C
I think that covers it, though having an explicit error message for any other value of `i` will probably be better than the default unmatched case one.
Thanks.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com http://IvanMiljenovic.wordpress.com _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com http://IvanMiljenovic.wordpress.com

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. [1] http://stackoverflow.com/questions/6000511/better-way-to-define-an-enum-in-h... On Tue, Apr 26, 2016 at 1:40 PM, Magicloud Magiclouds < magicloud.magiclouds@gmail.com> wrote:
OK. So my understanding is there is no better (good-looking) code. Thank you all. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Rahul Muttineni
participants (4)
-
Ivan Lazar Miljenovic
-
Magicloud Magiclouds
-
Rahul Muttineni
-
William Yager