4 ms·
Optional 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
by platz 10y ago
Optional 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 agoAgain, you are arguing a different point than than one I was addressing.
- tigershark 10y agoOnly since Java 8. The coalesce operator is in c# since the version 3.5 or 4 I think and c# 6 introduced the null conditional operator that, as I said before, is objectively nicer than the verbose Java optional. Before c# 6 I used this simple extension method: 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)