Exploring Explicit Nulls in Dotty

Another bit of interesting Dotty news, for those who want to know what might be coming up: there is an experimental but serious project to make null explicit in Scala 3.0.

This is really, really big, if they can pull it off. null has long been known as “the billion-dollar mistake” — it seemed like a good idea at the time, but has bedeviled the programming community for decades, because it pokes a gigantic hole in type safety. You may think you have a value of type Foo, but still find yourself with a null unexpectedly, leading to a NullPointerException.

Idiomatic Scala generally uses Option instead of null most of the time, but there are times when you can’t avoid it, particularly when interacting with Java or JavaScript, since some libraries either require you to pass null as a parameter, or which return it from some function calls. So we currently live with the rule that any reference type might contain null, and we just try to keep our API code clean to avoid that actually happening.

This project seeks to change that: to make it illegal to put null into a reference type unless you explicitly say that there might be one there. This way, we can lift this critical knowledge, of whether a value might be null or not, up into the type system, and we can program against it in a much more robust way.

I must emphasize that this is still a very early draft — the details are likely to change a lot, and it might not make it into Dotty at all. But I’m rooting for it: IMO, it’s a clear improvement if they can get it to consistently work.

Details can be found at several places: