For languages where the compiler doesn’t help, is there any benefit? I’ve seen Option in Java projects where it just seems to add one more layer of could-be-null.
They make the code slower, harder to read and don't really solve any problem.
And syntactically, it's not a pleasure to use, but that's more a fault of Java being so verbose and limited in terms of the typesystem, not a fault of the Optional type.
I think either a null-delegating operator (like C#'s foo.?bar ); or a variable modifier (a bit like 'final') that doesn't permit nulls would make far more sense in the context of the language at large. In fact, the approach is commonly enforced through annotations and static analysis. Built-in support for the construct would be very beneficial.
(various bits of perl IRC have adopted the mallet gnome as an idiom over the years and it seems to convey the "please don't" point without being overly combative ;)
if let Some(new_variable) = method_that_returns_optional_value {
println!("value: {new_variable}")
} else {
println!("{new_variable} is not in scope, this wont't compile");
}
I find it powerful. "if-let" and "while-let" are part of the language. OTOH rustaceans are happy with and constantly reasoning (constantly fighting the borrowchecker?) about scopes and if a variable / it's intended value is accessible at a certain line of the source code.