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:
int? x = null;
int y = x ?? 0;
int z = x.HasValue ? x.Value : 0;
if(x.HasValue && x.Value == 42){}
C# definitely doesn't have checked exceptions.edit:
For reference types, as compared to value types, c# has the null coalescing operator
List<string> y = someList() ?? new List<string>;
and the null conditional operator int? length = customers?.Length; // null if customers is null
which are defintely not Optional, but are what I use.[0] - https://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx
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...
I don't think the author has a beef with easy outs, but with accidental outs. Specifically with regards to the JVM, too, there are some practical security ramifications, with regards to running plugins.
You absolutely can:
Microsoft (R) F# Interactive version 14.0.23413.0
Copyright (c) Microsoft Corporation. All Rights Reserved.
For help type #help;;
> let s:string = null ;;
val s : string = nullNow, if a programming language has error aware return types - using algebraic data types or multiple dispatch - that's a better solution than both. But in the absence of that, give me checked exceptions over unchecked ones any day.
You probably would never write this, and your colleagues would never write this. That reasoning is good to apply in many situations. I in fact do this all the time; it’s a huge part of my research agenda. But you have to be careful where you apply it. Soundness is a security guarantee, not a usability concern. What matters is if someone can write this, because then that someone can get around the security measures that people have placed their confidence in and do whatever they would like to do. In the case of soundness, it’s the malicious programmer you should be worried, not just you and your friends.
I really hate this attitude. Programming languages should be designed to be safe and strict by default, but with easy outs. I utterly reject the notion that we should design languages first and foremost to protect us from people can't program that well or aren't careful when they write code.
Do we always have to appeal to the lowest common denominator? Empower the developers that care rather than trying to turn this into an unskilled profession.