I have a data type which I’m using to represent a wxAny object in wxHaskell, currently I only support wxAnys which contain a String or an Int, thus:
data Any
= IsString String
| IsInt Int
| IsUndefined
I need a function (a -> Any) and I’m wondering if I can do it elegantly using Data.Typeable, or if anyone can suggest another approach?
You can do it relatively simply by combining the
castfunction with pattern guards:That does require that the input be an instance of
Typeable, but most standard types have aderiving Typeableclause so it’s usually not a problem.