(no title)
sjustinas | 3 years ago
> Option and Result are now separate types: ?Foo and !Foo respectively. Old code will continue working for 1 year and will result in a warning/hint.
Yet, doc/docs.md still states:
> V combines Option and Result into one type, so you don't need to decide which one to use.
Is there an up-to-date doc, or how does one find out more about these types? I found parts of the RFC overhauling the error handling [0] pretty bizarre, so I'm interested to see how the implementation turned out.
[0]: https://github.com/vlang/rfcs/pull/7
edit: linked to the wrong RFC initially.
wwalexander|3 years ago
There are some obvious cases where a none optional is separate from failure, like a hash map lookup or an element from an iterator. But I often also see optionals used to signal e.g. an unparseable string, where it would seem equally reasonable to return a Result or throw an error.
I’ve been writing a lot of Swift lately, and because it uses `throws` rather than a Result type, it’s made the often arbitrary choice of returning an optional value or raising an error more noticeable to me. Especially when using `async`, as pretty much everything declared `async` also throws, so if an Optional is actually what you want to return, you get some fairly gross boilerplate at the site of use:
This has turned into a bit of a ramble but my main point is that implementing Optional as a Result with void error type makes a lot of sense in my head.amedvednikov|3 years ago
I don't think any other language has them merged.
There were issues later with the 3 states (Ok, Error, none) instead of a simple binary state.
Since the `or {}` block handles the "not Ok", developers had to check whether it was an Error or a `none`, it's not pretty.
amedvednikov|3 years ago