[Haskell-cafe] When using Cereal, what is the right way to write `get` for multi-constructor type?
Magicloud Magiclouds
magicloud.magiclouds at gmail.com
Tue Apr 26 08:10:49 UTC 2016
OK. So my understanding is there is no better (good-looking) code. Thank
you all.
William Yager <will.yager at gmail.com>于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 <
> ivan.miljenovic at gmail.com> wrote:
>
>> On 26 April 2016 at 15:39, Magicloud Magiclouds
>> <magicloud.magiclouds at gmail.com> 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 at haskell.org
>> > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> >
>>
>>
>>
>> --
>> Ivan Lazar Miljenovic
>> Ivan.Miljenovic at gmail.com
>> http://IvanMiljenovic.wordpress.com
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160426/5464c65b/attachment.html>
More information about the Haskell-Cafe
mailing list