[Haskell-beginners] Getting a variables type as a string at runtime

Gareth Morgan gmorgan1984 at gmail.com
Sun Jan 12 14:04:40 UTC 2014


Bit of a simple question but I'm struggling to find information on this via
Google.

I have a sum type, when the input is type A I want to process it, when it
is type B I want to return MyError "Expected type A but actually got type
B". However I also have C,D,E,F, etc which should also return similar
errors so don't want to hardcode this string.

Is there a function I can call on a variable to get the name of the data
constructor that created it? I could create a typeName function but I want
to avoid boiler plate if I can.

For reference what I'm actually doing is parsing Java class files. The
constant pool I've represented as a map of a sum ConstantPoolEntry type
(the Java constant pool for some reason skips an index on some types so
can't be a simple list). Certain Java class file entries have a reference
to the constant pool that must be of a specific constant type so I want to
report badly formed entries to the user.

Thanks,
Gareth
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140112/17b51ae4/attachment.html>


More information about the Beginners mailing list