[Haskell-cafe] Read instance for constructors?

gb gbwey9 at gmail.com
Mon Mar 10 22:47:26 UTC 2014


> 
> 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
> 
> A 1
> 
> of type D, etc.
> 
> Many thanks,
> S.
> 

Here's one way: 

import qualified Data.Generics.Builders as B
import Data.Generics.Aliases 

fromConstrB (B.empty `extB` (12::Int)) (f "B")::D
>>> B 12

fromConstrB B.empty (f "B")::D
>>> B 0




More information about the Haskell-Cafe mailing list