Live data from Hacker News

What if null was an Object in Java?

donraab.medium.com

101–110 of 164 posts

Re: What if null was an Object in Java?

#102
post #61
post #51

Earlier quoted context omitted.

Null is the absence of a value. How do to distinguish 0 from no value?

The point is to eliminate the idea of an absence of a value. A variable is always assigned to a value, but there is a special value called null which behaves as a kind of sentinel value whose methods all return the null value for their respective type.

Yes - but as we keep repeating - if you do that why would you use null as a possible state to begin with? Not specific to Java, but in general.

E.g a boolean in java has two states true/false while a Boolean with capital B has 3 states true/false/null.

In that context you can choose type to represent how many states you have. E.g if it’s a field representing a cache of a bool value you can represent “not yet calculated” with null. It you were to magically convert null to false for a Boolean it only has two states! It’s now unusable for the purpose.

Re: What if null was an Object in Java?

#103
Java developers introduced Optional for this very case, but Optional's API enforces stream style constructions. Moreover you must handle nulls sometimes (foreign API calls), and then you might run into type checking problems, NPE when put into a Map.of() etc.

For me the given examples from Smalltalk are more appealing. I would trade null for nil, or at least have some nullsafe construction like .? in swift

Re: What if null was an Object in Java?

#104

The problem isn't really that null isn't an object, it's that the type system allows any object reference of a particular class to also be null. After using Typescript for the past couple years, it's such a joy when you define a variable of type Foo and know it won't contain null. Granted, TS also has to deal with undefined vs null weirdness (and even more weirdness in that an object not containing a property is subt…

I'm writing some powershell at the moment, after a few weeks of work I realized that sometimes my return types for some functions would change even though I explicitely declare them (for exemple a 'hashtable' would become a 'System Object[]' which is short for 'table full of garbage').

Turns out some ops without assignment and some printed messages would go into the pipeline (as in, to be piped in with `|`) and become part of the return value of the functions ; to make that work powershell changes the return type, appends the message or the non-assigned op to the hashtable and returns that as an object table.

To this day I'm not aware of any language doing this kind of thing, but maybe js does ? Anyway I fear I'm going to get some grey hair because of that.

Re: What if null was an Object in Java?

#105

Earlier quoted context omitted.

Null should be valid. Kotlin solved Java's problem by making it a compiler error if a value that can be null isn't checked and shown to be null or the actual value, eliminating an entire class of exceptions.

I’m not familiar enough with kotlin to comment fully but from your description the checker framework [0] appears to do the same thing in Java. I confess I’m not fond of checker framework. I find the error messages can be obtuse but it is very effective. 0 - https://checkerframework.org/

Kotlin supports nullability on the level of the type system, it is similar to TypeScript in this respect.

The problem with nullability annotations in Java is that they are not enforced, and there is no commonly adopted standard. There are like ten competing libraries with similar annotations. There was JSR 305 ("Annotations for Software Defect Detection"), but it has been dormant. When you import a third party library, you never know what kind of nullability annotations it uses and if it uses them at all.

Re: What if null was an Object in Java?

#106

What if every null reference was an instance of a Null Object pattern? https://en.wikipedia.org/wiki/Null_object_pattern Eliminates null checks, Optionals, and NPEs. Probably moots annotations like @NotNull too, but maybe some use cases need those for client APIs. Makes iterating data structures like graphs simple. Faster too, because the JIT NOP a Null Object's methods. (Mostly; profile to confirm, then tweak as nee…

Objective-C does that but it has the effect of separating serious consequences from their causes. Places that really require non-null have a much more difficult time finding where the null value originated.

And since Objective-C contained a lot of C, it really just separated null dereferences from their causes making debugging much harder.

Re: What if null was an Object in Java?

#107

Objective-c allows you to send messages to null objects. On one hand it allows for a form of null-coalescing, but on the other it allows bugs to slip in and get the program into an unexpected state, whereas a more rigorous treatment would result in a more deterministic crash.

It is part of what makes Objective-C phenomenally good for rapid development in the hands of competent developers. However, as team, and codebase sizes increase, it allows for subtle bugs by less competent developers.

It’s also very much related to its use in GUI hierarchies: GUI objects generally have a lot of properties and behaviours, most of which you often don’t care for at any given point. Nil being a shorthand for “do nothing” without needing explicit checking is very convenient.

Re: What if null was an Object in Java?

#108
post #61
post #51

Earlier quoted context omitted.

Null is the absence of a value. How do to distinguish 0 from no value?

The point is to eliminate the idea of an absence of a value. A variable is always assigned to a value, but there is a special value called null which behaves as a kind of sentinel value whose methods all return the null value for their respective type.

You can do that with some types, but making 0 be that sentinel is completely bonkers.

You need a 257th value to have a byte sentinel.

Re: What if null was an Object in Java?

#109
post #63

If null-safety on the JVM is important to you, just use Kotlin.

Just use Kotlin. I'm honestly not seeing any need nor benefit in writing Java anymore.

I‘d say its the other way around, Java is closing the gap, and I say that as a Kotlin fan. Nullability in the type system is the big remaining advantage.

Re: What if null was an Object in Java?

#110
post #44

Earlier quoted context omitted.

I'm going to be a bit pedantic here: There is a semantic difference between Option > and Option . If I intend to retrieve a setting from a file, the former allows me to differentiate between a missing file or a missing setting, while the latter destroys that information. i.e.: There are 3 possible cases, while only 2 can be represented. So T? doesn't compose, while Option does, which I'd consider a big difference. Ho…

That sounds like you should be using Result to handle the two E cases you are describing. Success with Ok(T), with Err(Missing File) plus Err(Missing Setting).

Probably. A case I see more often is a dictionary with nullable values. A T? return can't distinguish key missing or key present with null value.

Which leads to a bit of an argument - nesting gives poor ergonomics, while collapsing impacts the ability to write generic code.

Post reply on HN