runtime reflection for classes

Donald Bruce Stewart dons at
Thu Feb 9 08:15:34 EST 2006

> With Data.Generics, we can get an object's type, constructor and fields.
> I think there should be way to get the object's class(es) and methods.
> E. g. I want to find out whether the object's type implements Show,
> and if so, call the show  method (and if not, call some replacement).
> See
> see also

Depending on how evil you are, you can already do this. Call the show,
and if it doesn't exist you can catch the exception that is thrown (a 
NoMethodError). Lambdabot actually does this when doing some fun dynamic
composition of plugins.

Here's a sample:

                (process m a b cmd str)
                (\ex -> case (ex :: IRCError) of
                            (IRCRaised (NoMethodError _)) -> process_ m cmd str
                            _ -> throwError ex))

Cheers, Don

More information about the Haskell-prime mailing list