dynamic types
Andrew J Bromage
ajb@spamcop.net
Wed, 15 Jan 2003 10:06:14 +1100
G'day all.
On Tue, Jan 14, 2003 at 07:13:57PM +1100, Fergus Henderson wrote:
> That's not the only problem. The other problem is that because
> `Typeable' instances aren't built-in, `fromDynamic' is not type-safe.
> The implementation of `fromDynamic' calls `typeOf' and then if the types
> match, it does an unsafe cast. If `typeOf' lies, then `fromDynamic' may
> break type safety.
First, an unsupported assertion: In any declarative language
implementation intended to be practical, there will inevitably be a
place or two where it is possible to lie to the compiler, thus
potentially making it break various language rules.
GHC has it here, and also in the "RULES" pragma. Mercury (to pick but
one other example) has the "promise" pragma which amounts to the same
thing.
I surely don't need to quote it again, but I will anyway:
"If you lie to your compiler, it will get its revenge."
-- Henry Spencer
Secondly, you're absolutely right:
> Yes, it would be nice to have a built-in, type-safe, version of Dynamic
> in Haskell 2.
And so is Glynn:
| It would also be nice if it were a little less abstract; e.g. either
| exposing the constructors for Dynamic and TypeRep or providing
| suitable accessors would be preferable to having to parse the result
| of "show".
What You Really Want(tm) is some kind of controlled reflection and
meta-interpretation mechanism which makes it unnecessary for
automatically derivable typeclasses like "Show", "Eq", "Enum" and
"Typeable" to require special compiler support.
Cheers,
Andrew Bromage