Idiomatic way to express errors without resorting to exceptions
Sebastiaan Koppe
mail at skoppe.eu
Sat Feb 29 15:23:02 UTC 2020
On Saturday, 29 February 2020 at 13:40:11 UTC, Adnan wrote:
> On Saturday, 29 February 2020 at 13:03:21 UTC, Sebastiaan Koppe
> wrote:
>> On Saturday, 29 February 2020 at 12:50:59 UTC, Adnan wrote:
>>> * Option!T from the optional package: Has even worse problem
>>> IMO. Not only it allows None + int but also it returns a
>>> `[]`. This API is not to my liking. You could say well
>>> Haskell has fmap for Optional etc, and I am aware of that, so
>>> does Rust with map etc. But I am talking about basic things:
>>> like `+`.
>>
>> I would argue it is one of its strengths.
>
> Doesn't that pretty much defeat the entire purpose of
> type-guarding a potentially error/absent value?
Like I said, I don't use optionals when I care about errors. That
is not what they are designed for.
If I want to type-guard potential errors I will use SumType!(T,
Error). It forces you to handle both cases, either at compile
time (with the match template), or at runtime (with the tryMatch
template).
The power of optionals, however, lies in the following:
```
list.first.doSomething();
```
Here doSomething will only be called when the list contains at
least one item. Here I don't care about cases where the list is
empty, I just want to doSomething if it isn't.
More information about the Digitalmars-d-learn
mailing list