Live data from Hacker News

What if null was an Object in Java?

donraab.medium.com

11–20 of 164 posts

Re: What if null was an Object in Java?

#11
post #7

I think the nullable reference type system in c# is a perfectly workable compromise for languages that had nullability from the very beginning. Once a code base uses them fully, most null-related bugs are gone. And it’s far from an unwieldy bolt-on.

The Nullable class is nice and I love to use it but it would be even better with an option type like in F#

Re: What if null was an Object in Java?

#12

I don't actually think it solves any problems. You still have to null check.

null would just mean the zero value instead of the absence of a value

  String foo = null;

  String bar = "";

  foo.equals(bar) --> true
This works well provided the data type has a sensible zero value like collection types

EDIT: I'm blocked from posting so I won't be responding further, thank you for discussion.

Re: What if null was an Object in Java?

#13
There is also the "Void" type.

> The {@code Void} class is an uninstantiable placeholder class to hold a reference to the {@code Class} object representing the Java keyword void.

When Java introduced Generics they re-used "Void" type. Method calls need to use "null" when "Void" is the type. So in a way, "type of null" is "Void".

Re: What if null was an Object in Java?

#14
post #8

Doesn't an Optional basically cover this case

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…

null can be avoided with a good linter

Re: What if null was an Object in Java?

#15
post #11
post #7

I think the nullable reference type system in c# is a perfectly workable compromise for languages that had nullability from the very beginning. Once a code base uses them fully, most null-related bugs are gone. And it’s far from an unwieldy bolt-on.

The Nullable class is nice and I love to use it but it would be even better with an option type like in F#

There isn’t any big difference between T? And Option semantically. Many code bases use an Option in C# to indicate a 0-or-1 object result, but refactor those to T? instead with nullable. It combines better e.g Option> doesn’t need to be handled manually.

Re: What if null was an Object in Java?

#16

I don't actually think it solves any problems. You still have to null check.

null would just mean the zero value instead of the absence of a value String foo = null; String bar = ""; foo.equals(bar) --> true This works well provided the data type has a sensible zero value like collection types EDIT: I'm blocked from posting so I won't be responding further, thank you for discussion.

In the end, you'll have a mixture of NULL and "" in your DB, and a couple of years later a piece of logic written in another language will fail spectacularly.

Re: What if null was an Object in Java?

#17
post #8

Doesn't an Optional basically cover this case

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…

Assuming that was the only constructor you defined on class Foo, and you used this.bar instead of foo.bar (latter won't compile), then the caller can't possibly get a Foo with bar set to null (except by reflection, and there are ways to prevent that). Moreover, even if new Bar() did throw an (unchecked) exception, the invariant would still hold, since Foo would rethrow the exception. This has always been the case, as far as I know.

Re: What if null was an Object in Java?

#18
post #8

Earlier quoted context omitted.

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…

null can be avoided with a good linter

Not avoided altogether. Static checkers cannot possibly follow all code paths, and they generally err on the side of false negatives rather than risking too many false positives causing people to disable them.

Re: What if null was an Object in Java?

#19

I don't actually think it solves any problems. You still have to null check.

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.

Re: What if null was an Object in Java?

#20
post #10

Ruby has a `NilClass` and the best/worst part of it is the to_s is "", to_i is 0, to_f is 0.0, to_a is [], to_h is {} It's incredibly clean and convenient until you wake up one morning and have no idea what is happening in your code

Indeed. One's greatest strength is also their greatest weakness.
Post reply on HN