News for hackers. Stuff that matters.
Re:Overhead of Returning Optional Values in Java and Rust by hn_throwaway_99 on Saturday October 16, 2021 @02:01PM (#28889922)
This is somewhat tangential, but in my opinion implementing Optionals as a "normal" wrapper type, as is done in Java, doesn't really help that much, and can in fact actively hurt, because (a) there is nothing that prevents an Optional variable from actually being null (I've seen a method with an Optional return type that returned null in some cases - obviously a horrible decision, but not prevented by the compiler nonetheless), (b) you don't get compiler support that knows, for example, that if you preface an empty check on the variable that it's then safe to get the value and (c) it makes code super-verbose and hurts readability.

You really need first class support for optionals, a la TypeScript and Kotlin, to get a benefit IMO.

Re:Overhead of Returning Optional Values in Java and Rust by hashmash on Saturday October 16, 2021 @02:43PM (#28890300)
> nothing that prevents an Optional variable from actually being null

That's why you're supposed to use Optional<Optional<X>>. ;-)

Re:Overhead of Returning Optional Values in Java and Rust by herbstein on Saturday October 16, 2021 @02:54PM (#28890381)
> This is somewhat tangential, but in my opinion implementing Optionals as a "normal" wrapper type, as is done in Java, doesn't really help that much

The great thing about Rust is that `Option` is a normal, generic type. You could very easily implement your own equivalent type.

Re:Overhead of Returning Optional Values in Java and Rust by mqus on Saturday October 16, 2021 @03:48PM (#28890855)
Not quite. The compiler does have the knowledge that it can use \0 for representing None in Option<Pointer> types. I'm not sure it can infer that for home-grown Option enums.

So it can optimize the memory overhead of Option<> away completely.

Re:Overhead of Returning Optional Values in Java and Rust by umanwizard on Saturday October 16, 2021 @03:59PM (#28890948)
That is called “niche optimization” and the compiler will in fact still do it for a hand-rolled Option type.
Re:Overhead of Returning Optional Values in Java and Rust by tialaramex on Saturday October 16, 2021 @07:11PM (#28892408)
Although, Rust only knows about certain obvious niches. It can reason that it can fit None in the same space as NonZeroU64 or a reference &T but it doesn't see how to do this with a hypothetical EvenU64 as I understand it because that "niche" is in the wrong place.
Re:Overhead of Returning Optional Values in Java and Rust by herbstein on Saturday October 16, 2021 @05:51PM (#28891918)
For anyone not sure, here is the proof.

https://godbolt.org/z/4zhWM9hTP

Re:Overhead of Returning Optional Values in Java and Rust by Jach on Saturday October 16, 2021 @03:21PM (#28890621)
My experience is they helped a lot. a) basically never happened (I have a vague recollection of catching one in code review, but usually my reviews would instead be "instead of returning null here, use Optional please" or figuring out a way to avoid nulls entirely (e.g. empty list when returning collections)) b) I think both Eclipse and IntelliJ will issue a warning if you perform an Optional.get() without an isPresent() check c) it seems less verbose and more readable than null checks, especially if you have a map chain/are using Streams. It could be even nicer, like what Kotlin has, but it's an improvement.
Re:Overhead of Returning Optional Values in Java and Rust by heurisko on Saturday October 16, 2021 @03:10PM (#28890526)
Java Optionals are useful in Streams.

I've found I agree with the principle that Optionals only make sense for return values (and if the method returns null, that should be treated as a bug).

I've seen Optionals used for member variables, and they might as well just be null. (And are highlighted as such by IDEs like IntelliJ).

Re:Overhead of Returning Optional Values in Java and Rust by Zababa on Saturday October 16, 2021 @02:08PM (#28889975)
It works very well in ML-like languages. The problem is when you have a language where everything can be null. TS and Kotlin need first class support for optional because the underlying language has first class support for null.
Re:Overhead of Returning Optional Values in Java and Rust by foolfoolz on Saturday October 16, 2021 @02:06PM (#28889956)
- if you don’t use null this isn’t an issue

- it’s verbose because handling optional values safely is complex

Re:Overhead of Returning Optional Values in Java and Rust by hn_throwaway_99 on Saturday October 16, 2021 @02:16PM (#28890055)
> if you don’t use null this isn’t an issue

I mean, no shit. But the problem is that tons of existing Java APIs do use nulls, so you can't just pretend they don't exist, and importantly a major point of the optional construct is to get compile-time guarantees, and the Optional class doesn't give you that.

> it’s verbose because handling optional values safely is complex

No, it's not. Kotlin has optionals built into the language, and it's safer and much less verbose.

Re:Overhead of Returning Optional Values in Java and Rust by tadfisher on Saturday October 16, 2021 @11:53PM (#28893805)
Nit: Kotlin doesn't exactly have optionals, it has essentially a type modifier (?) that represents a "nullable" type, i.e. the sum of a type T and the value 'null'. This is indeed safer and terser, therefore strictly more powerful than Java's Optional type, but it begs a question: Why not allow users to create similar higher-kinded types instead of baking this one very specific type kind into the compiler?

Their recent decision to axe typeclasses in favor of multiple function receivers, which solves a small subset of problems that HKTs do in Scala and Haskell, make me look for greener pastures whenever I need them.

Re:Overhead of Returning Optional Values in Java and Rust by Zababa on Saturday October 16, 2021 @02:09PM (#28889978)
How is "handling optional values safely" complex? Do you have any examples of that?
Re:Overhead of Returning Optional Values in Java and Rust by imoverclocked on Saturday October 16, 2021 @03:57PM (#28890925)
First class support for nullable is more basic and needed.

Java On its own it doesn’t support this however there are ways of enforcing this in a Java codebase. Eg: Errorprone and NullAway comes to mind here.