Live data from Hacker News

What if null was an Object in Java?

donraab.medium.com

111–120 of 164 posts

Re: What if null was an Object in Java?

#111
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…

> 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 Does nesting Option's really has practical use or does it quickly become confusing? In your example, Option > return type doesn't tell me by itself that this differentiates between a missing file or a missing setting. I would ne…

The biggest impact is on generic code - you don't want supplying Foo? as T rather than Foo to mean that there are side effects where successful returns and error cases are both represented by null.

Re: What if null was an Object in Java?

#112

Has anyone tried to make null as a function? In Julia, there is a `get(null::Function, collection, key)` method which can be used in a syntax: value = get(collection, key) do error(“$key in the collection not found”) end This syntax has captivated my attention lately as it seems like a viable alternative to doing an explicit null check before using the value.

That’s just a default callback.

Actually using this instead of nulls in any sort of general case would be using CPS for all faillible APIs, and that sounds heinous.

Re: What if null was an Object in Java?

#113
The only real problem with null, is not having a real optional type in Java. If you have it, 99,999% of null errors go away.

Java Optional std lib class, instead of a language (typesystem level) feature was one of biggest mistakes in its recent history.

Re: What if null was an Object in Java?

#114

The only real problem with null, is not having a real optional type in Java. If you have it, 99,999% of null errors go away. Java Optional std lib class, instead of a language (typesystem level) feature was one of biggest mistakes in its recent history.

At least they plan on introducing null safety for value objects via the „Null-Restricted Value Class Types“ JEP: https://openjdk.org/jeps/8316779

Re: What if null was an Object in Java?

#115
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

Those conversions make sense though. They all mean empty or null. It's what I would expect from a language like Ruby.

Re: What if null was an Object in Java?

#116
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.

It's funny because I really dislike the C# approach. It's not truly enforced - any code with nullable disabled can call public functions with `null`, even if on your side they were marked as non-null.

This is true, but you can fairly easily audit your solution for files/projects which have it disabled. And yeah, you need to verify the data at the boundaries of your trusted code - but that's the same situation as it's always been. Where NRT help is avoiding boilerplate null checks in the dense forest of your business logic.

Re: What if null was an Object in Java?

#117
Kotlin makes nullability part of the typesystem. It's a good reference for what would happen if Java did this better because if you are using Java you can just switch to Kotlin and experience this in your own code base (you can mix Java and Kotlin code easily).

You can actually write extension functions for nullable types and calling them on a null value does not cause a Nullpointer exception. This also works with Java classes.

For example, there's a CharSequence?.isNullOrBlank(): Boolean function in the Kotlin standard library that allows you to implement a null or blank check on strings. There's a similar isNullOrEmpty function on Lists (and yes, this does work for nullable generic types too).

There are many good reasons to switch from Java to Kotlin. But this is probably one of the bigger ones and genuinely low hanging fruit. Made me feel stupid after years of dealing with Java NPEs routinely and writing a lot of very verbose and needlessly defensive Java code in attempts to avoid them. All that cruft goes away with a proper language. Other languages that do this too are available of course. Pick one. Any one that isn't Java. Dealing with this stuff in 2024 is stupid.

Re: What if null was an Object in Java?

#118

I like the way null is handled in Java, I think it’s pretty well thought out, other than the small awkwardness with basic types. Contrast that with SQL, which had my external scorn for null != null So you can do WHERE table.col = Null…. Ugh

> Contrast that with SQL, which had my external scorn for null != null

For anyone not familiar, or who doesn't know the reason null behave like this in SQL: in this word, null essentially means "don't know". So you can't assume a = b when a and b are null. They could be different values, just that we don't have them.

This thing being called null, where null has another semantic in the languages from which we send SQL request is confusing, and I suspect many of us would prefer null behave the same way as everywhere else.

Re: What if null was an Object in Java?

#119
post #91

Earlier quoted context omitted.

The problem is, not all of these languages think that "" and null are equal.

I might choose to rephrase that as "the problem is, some of these languages think that "" and null are equal." :-)

Isn't the lack of strict equality a result of loose typing in those languages?

Re: What if null was an Object in Java?

#120
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.

I think there is a way to migrate to an union type myType | null. Instead.
Post reply on HN