4 ms·
Minor correction: Object value = null try { value = a.b.c.d } catch(NullPointerException x) {} and const value = a?.b?.c?.d; are not the s
by OptionOfT 9mo ago
Minor correction:
Object value = null
try {
value = a.b.c.d
} catch(NullPointerException x) {}
and
const value = a?.b?.c?.d;
are not the same. The ?. operator returns `undefined`, even if the operand is `null`.
Also, Optional<T> in Java is kinda weird. It's a mechanism to enforce null checks.
I much prefer pattern matching like in F# and Rust, where you get an Option<T>:
https://fsharpforfunandprofit.com/posts/the-option-type/ https://fsharpforfunandprofit.com/posts/the-option-type/
The ergonomics of matching and only having the variable when it's actually Some(_) makes it very natural, so much so that switching languages feels painful, as I'm constantly doubting myself. I don't need to read documentation about whether the output CAN be null/None. If it can be, it is encoded in the type system.
This is unlike Java. Many types can still just return null.