runtime reflection for classes

Donald Bruce Stewart dons at cse.unsw.edu.au
Thu Feb 9 08:15:34 EST 2006


waldmann:
> 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 http://www.haskell.org//pipermail/haskell/2005-November/016992.html
> see also http://java.sun.com/docs/books/tutorial/reflect/

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:

        catchError
                (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