[Template-haskell] Using a Typ as a Type

Simon Peyton-Jones simonpj@microsoft.com
Wed, 3 Sep 2003 09:45:18 +0100


| Is there any way that I can use a Typ (i.e., a reified Type) as a Type
inside
| a template so that I can use the Typ to select an instance of a type
class.
|=20
| For example, I'd like to be able to write (this doesn't parse):
|=20
|   $(test [t[ Float ]] "1.0")
|=20
|   test :: Q Typ -> String -> Q [Dec]
|   test qty x =3D do
|     ty <- qty
|     print (read x :: $ty)    -- The ':: $ty' part is the important bit
|     return []

I've Sean's reply and Alastair's reply to that, and I still can't figure
out what you are trying to do.  The above code looks weird. =20
	test is a Q [Dec], but you are doing a print in the middle of
it.  Why?

In principle you can certainly say:

	$(test [t| Float ]] "2.0")

	test :: Q Type -> String -> Q [Dec]
	test qty x =3D [| read (x::$qty) |]

so that=20
	$(test [t| Float ]] "2.0")
=3D=3D=3D>
	read ("2.0" :: Float)


TH doesn't support type splices today; that's one of the things I'm
actively working on.  But I'm not sure if this is what you meant.

Simon