5 ms·
with it's Optional type, 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 t
by dsp1234 10y ago
with it's Optional type,
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 https://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx
- sremani 10y agoYes, Nullable was introduced in 2005 with C# 2.0 if memory serves me right. Checked Exceptions was a design choice, http://www.artima.com/intv/handcuffs.html http://www.artima.com/intv/handcuffs.html
- jpfed 10y agoI'm unfamiliar with Java's Optional type, but the .NET type Nullable can only be parameterized over value types. It is there to add the concept of null to value types, not allow you to more precisely characterize the nullability of reference types.
- platz 10y agoOptional in Java is just a library. If you want to to use Optional in C#, there is no reason to believe that that is not possible due to some issue that makes it only available to Java. https://github.com/louthy/language-ext/tree/master/LanguageExt.Core https://github.com/louthy/language-ext/tree/master/LanguageE...
- jpfed 10y agoI didn't imply it was desired or wasn't possible...? Another reply to my comment clarified that Optional also only applies to value types, which makes it analogous to Nullable. There's no reason to port it (as opposed to any other implementation of an Optional type) over. 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 }
- lacampbell 10y agoAnd in Java they're in the the standard library. Which is going to go down better - simply using something official that every java library program will already have, or trying to explain to your colleagues why all your methods are returning something defined in this obscure library you added to all your builds?
- platz 10y agothis is a different question than the premise you initially outlined "It's much easier to write robust code in Java - with it's Optional type... than in C#."
- lacampbell 10y agoWhy the pedantry? C# is my day job - I'd love to be able to use options but I would have a hell of a time getting it cleared. 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#.
- platz 10y agoYou seem to be making several arguments at once. My only point is that C# has no problem implementing java's Optional, which some other folks seemed confused about, and there are already libraries that do so. If there is a social/cultural issue too, I was not commenting on that, or commenting on the usefulness of checked exceptions.
- lacampbell 10y agoLook how many people here are advocating for the nullable hack because they don't understand what an option is. With java the discussion could be side stepped entirely, since they added proper support and put it in the standard lib.
- platz 10y ago
- dsp1234 10y agoedit: this is wrong, I was misreading the documentation, which states that the Optional class is a value type, not the contents of the Optional class. 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.html https://docs.oracle.com/javase/8/docs/api/java/util/Optional...
- lacampbell 10y agoNullables work at a pinch, but they are a poor mans option type. http://fsharpforfunandprofit.com/posts/the-option-type/ http://fsharpforfunandprofit.com/posts/the-option-type/ Scroll down to "Option vs. Null vs. Nullable" for an explanation of the differences.