Annotations and Template Haskell
Bertram Felgenhauer
bertram.felgenhauer at googlemail.com
Mon Mar 15 08:19:27 EDT 2010
Hello Simon,
> I'm not sure what you intend here. Can you be more concrete?
A concrete example could look like this,
{-# ANN T $(defaultValue [| T { foo = 1 } |]) #-}
data T = T { foo :: Int, baz :: Int }
where defaultValue :: Q Exp -> Q Exp would encode the value in a
suitable form. The intention is to specify default values for some
fields of the record.
Then, later, I want to generate a parser for T, that takes the default
value into consideration, in order to make the foo field optional and
the baz field mandatory.
$(deriveParser ''T)
Inside deriveParser, I want to access the annotation of T. That
requires a function like
getAnnotations :: (Typeable a, Binary a) => Name -> Q [a]
which does not exist. So the first question is,
Would adding this function be feasible? If yes, what sort of stage
restrictions would be required to make it work?
Continuing the example above, I can achieve the same effect by passing
the default value in some form as an argument to deriveParser. But I'd
prefer using an annotation because then I only have to specify that
value once even if there are several chunks of generated code that rely
on a default value. A second advantage is that an unused annotation does
not add any clutter to the code at all -- it can be completely optional.
So in my eyes this feature would be ideal for specifying exceptional
handling of a few data types (or functions or constructors) from
Template Haskell code.
Of course there are disadvantages as well, foremost that it'd be yet
another ghc specific extension. So the second question is, is the idea
really useful enough to justify being implemented?
regards,
Bertram
[annotations] http://hackage.haskell.org/trac/ghc/wiki/Annotations
More information about the Glasgow-haskell-users
mailing list