[Template-haskell] Stuck... is it even possible?

Tim Newsham newsham at lava.net
Fri Jan 20 12:35:03 EST 2006


> first, `flen` must be monadic computation because it needs access to the Q
> monad
>
> second, define it recursively:
>
> flen t | t=='Word8  = return 1
> flen t | t=='Word16 = return 2
> flen t = do -- Get information about type t, assuming it is declared as
>            -- one-constructor "data", such as "data T = T Int16 Word32"
>            TyConI (DataD _ _ _ [constructor] _)  <-  reify t
>            -- Get types of individual fields
>            let fields = fieldTypes constructor
>            -- Calculate len for each field
>            flens <- mapM flen fields
>            return (sum flens)

Thank you.  This makes a lot of sense.  My only problem with this is
the declarations of the base types in the generator:
    flen t | t == 'Word8 = return 1
This precludes someone from adding other base types without modifying
the generator.

>> I have a record name and a field name (and a field type name), all
>> as strings.
>
> passing them as the strings is not good idea. it's better to keep them
> as Name values (Name type is defined in TH)

*nod*  The strings were my first step to get things working before I
explore more TH syntax.

> Bulat                            mailto:bulatz at HotPOP.com

Thank you for your insights.

Tim Newsham
http://www.lava.net/~newsham/


More information about the template-haskell mailing list