Checked Exceptions was a design choice, http://www.artima.com/intv/handcuffs.html
http://fsharpforfunandprofit.com/posts/the-option-type/
Scroll down to "Option vs. Null vs. Nullable" for an explanation of the differences.
What is really necessary is an equivalent for reference types.
struct Maybe<T> where T : class {
private bool _isInitialized;
private T _data;
public Maybe(T data) {
_isInitialized = (data != null);
_data = data;
}
// ...etc
}no - Java's Optional applies to reference types.
public static TDest IfNotNull<TSource, TDest>(this TSource obj, Func<TSource, TDest> func)
{
return obj != null ? func(obj) : default(TDest);
}
Or something similar (writing c# code on the iPad before sleeping is not exactly simple), inspired by some blog post ages ago.
Just adding these three line of codes in a static class in the solution will enable you to write something like this: var id = order.IfNotNull(o => o.Customer).IfNotNull(c => c.Id) ?? "INVALID"
Still not as good as the null check operator but comparable with the Java optional (arguably better from my point of view) String id = order.map(o -> o.Customer).map(c -> c.Id).orElse("INVALID");
The Java code is actually shorter and works straight out of the box, so I don't know why you're calling Optionals "verbose".I think that was the point he was making, that it's always been possible, and now is simple.
string? id = "Name";
While you can do stuff like this: int x = 0;
int? y = x;
Again, F# has nullable types and options, and the fact that no one in F# uses nullable types should tell you something.Your quote also omitted checked exceptions, which is another reason why it's easier than me to write robust code in Java than in C#.
According the documentation, Optional is only valid with value types[0]:
"This is a value-based class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances of Optional may have unpredictable results and should be avoided."
Again, I have no experience, so if this is wrong, then additional corrective references would be awesome.
[0] - https://docs.oracle.com/javase/8/docs/api/java/util/Optional...
C# has Nullable<T> (aka T?)[0]. I'm not a Java expert, but they appear to be the same (represent a value or a null value, allow you to inspect for the presence of a value or null).
ex:
C# definitely doesn't have checked exceptions.edit:
For reference types, as compared to value types, c# has the null coalescing operator
and the null conditional operator which are defintely not Optional, but are what I use.[0] - https://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx