Yes, this can be handled with help of type classes or GADTs or you can even use cast
leveraging Data.Typeable
. But all those thing are useful when you know Haskell well, and when you know what you're doing exactly.
In your case, you should really try to understand how Haskell works in general. Haskell is about imposing wise invariants that make it impossible to write “bad” programs (well, that's how I put it). This is done via type system, and without those invariants attached you cannot really do anything.
So now, you cannot do much with a thing of type a
, because you don't know much about it. If you want to return value from function that can be either Int
or Bool
(that's actually possible see existential quantification), you will be able to perform actions on that value that can be performed on Int
and on Bool
. You can get textual representation of that value for example.
Chances are you want more than that. In this case you probably shouldn't try to return Int
and Bool
at the same time without a way to understand what exactly you have as a result.
So, you can do the following for example:
getVar :: Value -> Either Int Bool
Or you can just dispatch:
case Value of
IntVal x -> doWhatYouWantToDoWithInteger x
BoolVal x -> doWhatYouWantToDoWithBoolean x
This is natural flow of Haskell. In every branch of program you now know what you're dealing with.
Hope it helps.
getVar
? - BergigetVar
? - BergiMaybe
, so that it can fail when the input is unexpected. - Bergi