5 ms·
Not the person you're asking, but I may have insight. Realistically, it's no different, checking if something is null or None. Yeah, some languages have a lot
by 0x457 3y ago
Not the person you're asking, but I may have insight.
Realistically, it's no different, checking if something is null or None. Yeah, some languages have a lot of sugar around optional thanks to rich type systems.
Imagine the following pseudo rust code:
if let Some(x) = x {}
is not that different from
if x != null {}
Or any other type of boolean check.
I think you have to be around optional long enough to understand the benefits.
- mrkeen 3y ago> Realistically, it's no different, checking if something is null or None. That is correct. However, what languages with null lack is a way to say "See this String here? This is actually a String - not the absence of a String". It is nonsense to encode both A and lack-of-A as A.
- 0x457 3y agoYeah, and it's annoying. One of my most hated things about proto3 - is this string empty or just not set? (they added optional back now, but everything is already optional)
- mathgladiator 3y agoBut it is different because you can't dereference x without the check. In other words, you can't really make an accident. My language has maybe<T>, and I have automatic "re-wrapping" (not sure if there is a better term). where For example, with record R { int x; } maybe<R> r; I can access r.x yet the type is maybe<int>.
- 0x457 3y agoTrue. I'm just playing devil's advocate here. In unsafe rust, technically deref you can for types that have niche optimization.
- Akronymus 3y agoWell, you can have nested options. Some(None) is different from a None It also has nice ergonomics with bind, map and such. And the fact that a None is not even comparable to an 'a.
- 0x457 3y agoOh, I'm not arguing about the usefulness of optional - I love them.