[Haskell-cafe] run-time type testing in haskell?

Robin Green greenrd at greenrd.org
Sat Apr 28 09:14:46 EDT 2007


On Sat, 28 Apr 2007 14:06:21 +0100
Eric <eeoam at ukfsn.org> wrote:

> In imperative languages one can test the type of a variable and
> downcast if necessary. Here's an example in Pseudojava:
> 
> T v := ... ;
> if (v instanceof T') T' v' := (T')v

In object-oriented languages you can achieve the same effect by
defining a method on each type which has different behaviour depending
on the type.

In Haskell you can achieve much the same effect by defining methods in
typeclasses and then defining instances for types. It's quite
analogous. This is the recommended way of doing it in Haskell.
-- 
Robin


More information about the Haskell-Cafe mailing list