News for hackers. Stuff that matters.
Re:New Features in Java 14 by Sharlin on Tuesday March 03, 2020 @05:46PM (#22479462)
In Java you're passed a reference to an object. Might it be null? Can it be null? Who knows! Mostly you're just going to ignore the possibility and hope for the best.

In Rust you're passed a reference to an object. Can it be null? No, it simply can't! There does not exist a magic "null" value for references. To represent the possible absence of a value, you explicitly encode it into the type system by using `Option<T>`.

Re:New Features in Java 14 by hcarvalhoalves on Tuesday March 03, 2020 @10:39PM (#22481021)
Just changed from having a bottom null type for all types to everything being a boxed Option type. It’s equivalent.

The actual difference is the reliance on pattern matching and the compiler enforcing coverage on those languages.

Re:New Features in Java 14 by LessDmesg on Wednesday March 04, 2020 @02:06AM (#22481776)
Option types don't have to be boxed and are specifically optimized in Rust and, if I remember correctly, in Haskell. The Nothing is represented much like a null pointer, i.e. with a special value that can't be dereferenced.
Re:New Features in Java 14 by tome on Wednesday March 04, 2020 @03:37AM (#22482104)
Not in Haskell, FYI. Maybes are boxed.
Re:New Features in Java 14 by james_s_tayler on Wednesday March 04, 2020 @03:21AM (#22482038)
In Java the Option of T type blows up the same way a NullPointerException does if you try to use the Option's value when it contains none.

I agree. In Java at least the semantics are practically identical.

I can't comment on Rust. I suspect it does a slightly better job.

Crystal is another language where there is no null. It's quite cool.

Re:New Features in Java 14 by steveklabnik on Wednesday March 04, 2020 @08:22AM (#22483500)
Rust refuses to compile, because an Option<T> type is a different type than T, so if you try to use it like one, you get a type error.
Re:New Features in Java 14 by swsieber on Wednesday March 04, 2020 @10:37AM (#22484619)
Java would also refuse to compile using an Optional<T> where T is expected.

Java and Rust are practically the same regarding the differences between Optional<T> and T (java), and Option<T> and T (rust). Optional<String> in Java has the `.get()` method which corresponds to the `.unwrap()` method in Rust. And in both are different different types than String in their respective languages.

But in Java you can have an Optional<String> that's null, not just empty. So that's three states - null, wrapping null, or wrapping a value.

I'd say the big difference is that in Rust you can have places that refuse nulls (e.g. using T instead of Option<T>), so you have well defined points of control/checking. That, combined with the difficulty in recovering from a panic (the equivalent of a null pointer exception when trying to unwrap a value that's missing) and the Result type, are Rusts real strength wrt. missing values.

Re:New Features in Java 14 by steveklabnik on Wednesday March 04, 2020 @11:42AM (#22485280)
I think we are saying the same thing, from slightly different perspectives. That Option can be nullable means that it can be used where it is not actually that type, without a type error, is what I’m saying.
Re:New Features in Java 14 by Sharlin on Wednesday March 04, 2020 @02:25AM (#22481840)
Huh, what? You don’t need or want most values to be potentially absent.
Re:New Features in Java 14 by thu2111 on Wednesday March 04, 2020 @05:29AM (#22482622)
Yes, the difference is a combination of types being non-null by default and compiler enforced checking.

This doesn't require an option type. Option types are actually quite awkward compared to language integration. Kotlin doesn't use an Option type yet still delivers all the same benefits as Rust/Haskell's approach, with benefits (e.g. zero overhead, integrated syntax). Note that Option type overhead isn't merely about heavyweight syntax and the need for the compiler to successfully scalarise: using language integration around standard null pointers means the CPU spends no time on checking the 'maybe' branch. If the type system wasn't violated and a value is present, it's used immediately without any additional instructions or code bloat. If it's not then that will trigger a page fault at some point in the CPU pipeline and transfer control to an exception handler. If the type system indicates nullability and you need to actually do more than just unwrap it, then you have to take a branch of course but it's fully inlined and cheap.

Re:New Features in Java 14 by strbean on Wednesday March 04, 2020 @04:14PM (#22488211)
Doesn't that approach make the "nothing branch" extremely expensive? Makes sense in scenarios where you really always expect Something and not Nothing. In practice, Options are very often used where something is optional, and you can expect to get Nothing a good percentage of the time. Using exceptions in such cases seems silly.
Re:New Features in Java 14 by thu2111 on Thursday March 05, 2020 @01:09PM (#22496242)
Yes if you expect nothing, you test for it obviously. The problem is when you want high quality errors for the case where you didn't expect it and you e.g. casted away the nullness, or it was casted away for you due to language interop.
Re:New Features in Java 14 by pjmlp on Wednesday March 04, 2020 @01:50AM (#22481712)
Integrate PMD and enable its errors as broken builds, done.
Re:New Features in Java 14 by rakoo on Wednesday March 04, 2020 @04:59AM (#22482479)
I know what an Optional is, the problem is that this pattern of wrapping stuff in Optionals seems to appear a lot, which means that in practice you have to do this little dance like you would in Java or, as others pointed out, try your luck with unwrap() and hope it goes well. In practice, it doesn't make coding much different.
Re:New Features in Java 14 by sgift on Wednesday March 04, 2020 @05:50AM (#22482707)
You seem to forget all the cases in Java where you didn't expect null to happen, but for some reason something was null and you get a NPE. Sure, if you check all values for null already the difference to now is not very big, but no one I know does this. Everyone has some intuition whether something "can" be null or not. Rust (and other languages without null) formalize the notion of "this cannot be null", so you don't have to depend on your intuition anymore - and cannot be wrong about it.

Regarding unwrap: If you tell the compiler "I know what I'm doing" and you don't know what you are doing then no one can help you. It's the same as putting casts everywhere when the compiler yells at you about incompatible types: If you lie to the compiler it will do your bidding, but your code will run into errors at runtime that you could have prevented at compile time. Again: No one can help you here.