3 ms·
In Haskell, what you call a union type is more often called "sum type", or "tagged union". You declare them this way (your syntax was quite good): data Int
by loup-vaillant 17y ago
In Haskell, what you call a union type is more often called "sum type", or "tagged union". You declare them this way (your syntax was quite good):
data IntOrNothing = AnInteger Int
| Nothing
You can use it that way:
test :: IntOrNothing -> bool -- optional type declaration
test AnInteger i -> True
test Nothing -> False
This is not overloading. This is the same function. I also can write it this way:
test :: IntOrNothing -> bool -- optional type declaration
test intOrNot = case intOrNot of
AnInteger i -> True
Nothing -> False
So, the "choice" of union type is in the world of values, not in the world of types. Therefore, it isn't particularly relevant in the case of a type-based search.