3 ms·
I often run into the opposite problem: There are union types, but I would really like sum types (i.e. tagged unions). The common case is a data structure where
by Skeime 3y ago
I often run into the opposite problem: There are union types, but I would really like sum types (i.e. tagged unions). The common case is a data structure where I cannot guarantee that the user will not want to use Null as a value in my data structure, but I also need to represent the absence of values myself. With a sum type, my absence would be None and the user’s absence would be Some(None). With union types, I need to somehow create an extra sentinel value myself.
- cubefox 3y agoSomething like String|NoAnswer|None doesn't sound too bad to me here...
- Skeime 3y agoExcept that NoAnswer has none of the language’s (and it’s library’s) support for the built-in None. And you might run into problems when trying nest the data structure, which might be a reasonable thing for your users to do.
- cubefox 3y agoI just think that the nesting offered by Option & Co has overall much more downsides than upsides compared to union types.
- DougBTX 3y agoThis isn’t just a problem with custom types, it affects generic iteration types too. With a next() -> Option<T> method, an Iterator can iterate over any type of element.
- cubefox 3y agoYeah, but there could be a type T for "any type" additional to union types, basically like TRUE in Boolean logic. (Theoretically you could also have F for "no type", intersection types (AND), negation types (not Foo), and any logical complex of those.)