dynamic types

Andrew J Bromage ajb@spamcop.net
Wed, 8 Jan 2003 10:35:20 +1100


G'day all.

On Wed, Jan 08, 2003 at 08:42:20AM +1100, Thomas Conway wrote:

> I'm fairly new to Haskell, but worked on Mercury until recently.
> Mercury has a type "univ" which might be declared something like:
> 
> data Univ = Univ a

The equivalent would be:

	data Univ = forall a. Univ a

> I believe (nth hand) that something similar has been done in haskell, but my 
> understanding is that it isn't in the standard library.

That would be Data.Dynamic:

	data Dynamic		-- abstract
	toDyn :: (Typeable a) => a -> Dynamic
	fromDynamic :: (Typeable a) => Dynamic -> Maybe a

(There is a certain kind of logic behind the inconsistent naming,
incidentally.)

The Typeable class basically implements RTTI explicitly:

	class Typeable a where
	  typeOf :: a -> TypeRep

Unfortunately, Typeable can't be derived automatically.  Maybe in
Haskell 2.  For further details, see:

	http://haskell.org/ghc/docs/latest/html/base/Data.Dynamic.html

Cheers,
Andrew Bromage