For this example, I feel that it is actually fairly ergonomic in languages that have an Option
type (like Rust), which can either be Some
value or no value (None
), and don’t normally have null
as a concept. It normalizes explicitly dealing with the None instead of having null
or hidden empty strings and such.
- 0 Posts
- 2 Comments
Joined 2 years ago
Cake day: June 5th, 2023
You are not logged in. If you use a Fediverse account that is able to follow users, you can follow this user.
It is fair to have a preference for exceptions. It sounds like there may be a misunderstanding on how
Option
works.Have you used languages that didn’t have
null
and hadOption
instead? If we look at Rust, you can’t forget not to check it: it is impossible to get theSome
of anOption
without dealing with theNone
. You can’t forget this. You can mess up in a lot of other ways, but you explicitly have to decide how to handle that potentialNone
case.If you want it to fail fast and obvious, there are ways to do this. For example you, you can use the
unwrap()
method to get the containedSome
value or panic if it isNone
,expect()
to do the same but with a custom panic message, the?
operator to get the containedSome
value or return the function withNone
, etc. Tangentially, these also work forResult
, which can beOk
orErr
.It is pretty common to use these methods in places where you always want to fail somewhere that you don’t expect should have a
None
or where you don’t want your code to deal with the consequences of something unexpected. You have decided this and live with the consequences, instead of it implicitly happening/you forgetting to deal with it.