News for hackers. Stuff that matters.
Re:Java Is Unsound: The Industry Perspective by jpfed on Tuesday January 24, 2017 @02:23PM (#13474585)
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.
Re:Java Is Unsound: The Industry Perspective by platz on Tuesday January 24, 2017 @02:48PM (#13474812)
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/LanguageE...
Re:Java Is Unsound: The Industry Perspective by jpfed on Tuesday January 24, 2017 @03:14PM (#13475033)
I 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
}
Re:Java Is Unsound: The Industry Perspective by platz on Tuesday January 24, 2017 @03:23PM (#13475106)
> Optional also only applies to value types

no - Java's Optional applies to reference types.

Re:Java Is Unsound: The Industry Perspective by dsp1234 on Tuesday January 24, 2017 @03:36PM (#13475226)
Yes, I was misreading the documentation. The Optional class itself is to be considered a value type, and reference equality on Optional objects shouldn't be done.
Re:Java Is Unsound: The Industry Perspective by lacampbell on Tuesday January 24, 2017 @05:06PM (#13476115)
And 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?
Re:Java Is Unsound: The Industry Perspective by tigershark on Tuesday January 24, 2017 @06:35PM (#13476862)
Only 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)
Re:Java Is Unsound: The Industry Perspective by lacampbell on Tuesday January 24, 2017 @07:00PM (#13477050)
How is it better? All you've done is re-implemented mapping over an option. Your C# code is this in Java:

    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".
Re:Java Is Unsound: The Industry Perspective by tigershark on Wednesday January 25, 2017 @04:04AM (#13479891)
The null check operator is much more concise than my pre-c# 6 solution. Moreover you can apply both my solution and the null check operator to any type, while for Java you have to specify that a field is optional (increasing the verbosity and moving the responsibility to who write the class) and you can't do it for types that you don't own. So for these types the only way is to create a wrapper type just to add the optional fields, and this brings the amount of boilerplate code through the roof. So, just to sum it up, contrary to what you say the c# solution works out of the box, is much shorter than the Java alternative and works for every type. Honestly I can't really see why Java solution is superior for you.
Re:Java Is Unsound: The Industry Perspective by vile on Tuesday January 24, 2017 @10:38PM (#13478249)
It's not shorter than out-of-the-box c# 6, which would be var id = order?.Customer?.Id ?? "INVALID";

I think that was the point he was making, that it's always been possible, and now is simple.

Re:Java Is Unsound: The Industry Perspective by lacampbell on Tuesday January 24, 2017 @11:37PM (#13478569)
Except that nullable types aren't the same thing as option types, so it's not doing the same thing. Nullables are much weaker than options. You can't do this for instance:

    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.
Re:Java Is Unsound: The Industry Perspective by platz on Tuesday January 24, 2017 @05:18PM (#13476244)
this 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#."
Re:Java Is Unsound: The Industry Perspective by lacampbell on Tuesday January 24, 2017 @09:11PM (#13477827)
Why 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#.

Re:Java Is Unsound: The Industry Perspective by platz on Tuesday January 24, 2017 @09:21PM (#13477872)
You 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.
Re:Java Is Unsound: The Industry Perspective by lacampbell on Tuesday January 24, 2017 @11:16PM (#13478461)
Look 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.
Re:Java Is Unsound: The Industry Perspective by platz on Tuesday January 24, 2017 @11:22PM (#13478489)
Again, you are arguing a different point than than one I was addressing.
Re:Java Is Unsound: The Industry Perspective by dsp1234 on Tuesday January 24, 2017 @02:52PM (#13474845)
edit: 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...