[Haskell-cafe] How to define a type in a TemplateHaskell function and use it in the same function?

Anthony Clayden anthony_clayden at clear.net.nz
Tue May 23 02:11:01 UTC 2017


> On Mon May 22 09:45:49 UTC 2017, Saurabh Nanda wrote:

> Is there any way to have as single TH function, define a
type,
> and use the type, as well? 

In short, no. You must define/compile the TH function
in a separate module, then import to where you want to use
it.

See second bullet of this:
https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#using-template-haskell

And any intro to TH will also explain why it's hard.

At first I was puzzled why you didn't get an error message
explaining that more clearly,
but I can see your usage of the TH function
is also incorrect.

You're trying to make a data declaration.
So that must appear at top level in the program.
It makes no sense to appear inside a monad.

Also I suspect the imported TH function
`makeAdaptorAndInstance` is trying to generate
an instance declaration.
This must also appear at top level,
not inside a monad.

> Cross-posted from StackOverflow: ...

Yes, that answer has also moved the $( ... )
to top level.


AntC


More information about the Haskell-Cafe mailing list