[Haskell-cafe] Read instance for constructors?
Semen Trygubenko / Семен Тригубенко
semen at trygub.com
Mon Mar 10 16:54:36 UTC 2014
On Mon, Mar 10, 2014 at 02:59:36PM +0100, Niklas Haas wrote:
> On Mon, 10 Mar 2014 14:54:14 +0100, Niklas Haas <haskell at nand.wakku.to> wrote:
> > 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.)
> Oops, you are asking about the other direction. Well, you're in luck
> here too - Data has readConstr :: DataType -> String -> Maybe Constr.
Great! But how do I recover the actual constructor? E.g.,
f :: String -> Constr
f s = fromMaybe (error "error in f") $ readConstr (dataTypeOf $ B 1) s
gives me back Data.Data.Constr (not D). I was hoping for something along the lines
f "A" $ 1
to get back a value
of type D, etc.
Семен Тригубенко http://trygub.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 196 bytes
Desc: not available
More information about the Haskell-Cafe