[Haskell-cafe] Read instance for constructors?

Niklas Haas haskell at nand.wakku.to
Mon Mar 10 13:54:14 UTC 2014


On Mon, 10 Mar 2014 13:11:25 +0000, Semen Trygubenko / Семен Тригубенко <semen at trygub.com> wrote:
> Dear Haskell-cafe,
> 
> When deriving (Read), only values can be read in.
> If one wants to be able to read in constructors, too, is there an easy way out?
> E.g., the code below works, but the extra book-keeping
> 
> f "A" = A
> ...
> 
> is unpleasant — perhaps there's a simpler solution?

You can derive Data using the DeriveDataTypeable extension and then use
the toConstr :: Data a => a -> Constr method to obtain the Constr (which
has a Show instance that in this case just returns "A", "B" etc.)


More information about the Haskell-Cafe mailing list