Comment

Comment by hedora

An Optional is just a tri-valued null (null, None, and Some), so no.

It'd be nice if Java had a concept of a never-null reference (like a C++ reference vs. a C++ pointer), but the @NotNull annotation wasn't enforced the last time I checked.

Also, there's no way for an object to express that invariant because encapsulation is so weak. Given only this constructor (and no reflection):

   Foo() { foo.bar = new Bar(); /* foo.bar is final; Bar() does not throw */ }
callers can still get an instance of Foo with bar set to null.

Anyway, null handling in java somehow manages to be worse than C, where you can at least inline a struct into another, statically guaranteeing the instance of the inlined struct exists.

I can't think of another statically typed language that screws this up so badly. It just keeps getting worse with stuff like Optional and @NotNull.

(Disclaimer: I haven't followed java for 4-5 years; it's possible they finally fixed this stuff.)

parent hedora 2 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton