7 ms·
I'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 valu
by jpfed 10y ago
I'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...