[Haskell-cafe] Introspection

Yves Parès limestrael at gmail.com
Fri Jul 22 14:04:08 CEST 2011


You can do that using the Typeable class and possibly the Dynamic type.
(Data.Typeable and Data.Dynamic in base)

When you have:

foo :: (Typeable a, Num a) => a -> a
foo x = case (cast x :: Maybe Int) of
    Just x' -> x + 12
    _ -> x

foo will accept every instance of Num but will add 12 to it only if it is an
Int.

Dynamic wraps an instance of Typeable.

2011/7/22 Patrick Browne <patrick.browne at dit.ie>

> Is it possible to access type information and perform either of the
> following in a Haskell program?
>
> 1) Find the type/class of a variable
> e.g. a type predicate:  is y of-type A, or is y of-Class A
>
> 2) Assert the type of a variable
> e.g. if y.length > 100 then y is of type big.
>
> Regards,
> Pat
>
>
> This message has been scanned for content and viruses by the DIT
> Information Services E-Mail Scanning Service, and is believed to be clean.
> http://www.dit.ie
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110722/06b9c509/attachment.htm>


More information about the Haskell-Cafe mailing list