Re:Why Checked Exceptions Failed
by Skeime on Saturday July 15, 2023 @09:26AM
(#36736528)
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.
[ Parent
| on HN ]
Something like String|NoAnswer|None doesn't sound too bad to me here...
This 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.
Yeah, 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.)
Except 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.