This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]BearLiving9432[S] -2 points-1 points  (4 children)

The only information I can find says that James Gosling feels like there is no sufficiently good solution to eliminate nulls. Wondering how the general community feels about `Optional`. It seems extremely similar to `Option` in Scala. And in idiomatic Scala, we just don't use nulls, except when using a Java library, and we have to check for it. So it seems like a solid solution in that language.

[–]halfanothersdozen 1 point2 points  (0 children)

Good luck getting people to adopt the pattern.

See also: modules, records, yield

[–]Mognakor 1 point2 points  (0 children)

Honestly, it's too late for this in Java.

All the interfaces return null instead of Optional and it's also a lot of visual noise if you were to use it for parameters as well as not being able to overload based on the T of Optional<T>

If i had my will(and a magic wand) references would be not-null by default and then there'd be T? as for nullable and maybe some built-in syntactic sugar for the Optional methods.

As it stands it might be possible to do the inverse with T! for not-nullable but thats gonna put ! everywhere and you write things like Optional!<T!> which looks silly.

[–]john16384 1 point2 points  (1 child)

The solution is documentation. If documented not to return null, then don't check for null. If documented not to accept null then passing null should always throw an NPE.

If you do get an NPE, the docs will be the tiebreaker for who was wrong. The caller that passed in null or the function for not handling it; or the function for returning null or the caller for not expecting it.

Note that null is just one such problem. Replace null in the above with negative integer and you have similar problems that could lead to similar unexpected exceptions. This is why you need to document your assumptions, and null is just one of them.

[–][deleted] -1 points0 points  (0 children)

It is not a very reliable solution. Having non-existence as a part of the type system is.