Here is something close to what you are asking for.
import Data.Data import Data.Dynamic import Data.Maybe data City = City String deriving (Data, Typeable, Show, Eq) data Street = Street String deriving (Data, Typeable, Show, Eq) data Addr = Addr { city :: City ,street :: Street} deriving (Show, Eq, Data, Typeable) class Foo a where genericConstr :: [(String,Dynamic)] -> a instance Foo Addr where genericConstr = buildAddr lf ls nm = fromMaybe (error $ nm ++ " not found") (lookup nm ls >>= fromDynamic) buildAddr ls = Addr {city = lf ls "city", street = lf ls "street"}
Download this, and in ghci:
*Foo> genericConstr [("street", toDyn (Street "Baker")), ("city", toDyn (City "London"))] :: Addr Addr {city = City "London", street = Street "Baker"}
It seems to me that this is a lot for me. This is complicated because Haskell requires all types to be allowed at compile time; in this case, you are trying to create types with runtime information (for example, the string "Address"). It is possible, but you will struggle with the type system at every step. I agree with Jason that using a parser is probably the best approach.
source share