Null-safety Part 3: Announcing ScalaNullSafe!
Existing Options
So, unfortunately there’s at least one issue with all of the different existing approaches. The best one in terms of performance is the explicit null-checks, and the only problem with that is that it’s hard to read and write; but what if we could make it easy to do so?
Enter the macro
This is where the idea for the macro-based approach comes in. It allows us to create, what is essentially, a source code
-> source code
level transformation at compile time; or in layman’s terms, a code re-writing tool.
With this power, we can specify the property we’re trying to access, and have the macro rewrite it for us, as the fully explicitly null-checked version.
An example:
With this approach we get the best possible solution, something that is null-safe, easy to read and write, and efficient!
Null-safe | Readable / Writable | Efficient | |
---|---|---|---|
ScalaNullSafe | ️ | ️ | |
Normal access | ️ | ️ | |
Explicit null-checks | ️ | ️ | |
Option flatMap | ️ | ||
For loop flatMap | ️ | ️ | |
Null-safe navigator | ️ | ️ | ️ |
Try-catch NPE | ️ | ️ | ️ |
Monocle Optional (lenses) | ️ | ||
thoughtworks NullSafe DSL | ️ | ️ | ️ |
Key: ️ = Good, = Sub-optimal, = Bad
How to get it
I’ve published the source code for the macro on github, and I’ve also published the jar on maven, so it can easily be incorporated into other projects.
To add it to your project, just add the dependency
and then import
and you’re good to go!
More features
There’s also two other variants of the macro:
Opt macro
opt
, which is useful for interoping with Java code and work as follows:
notNull macro
and notNull
which works like this:
Safe translation
All of the above work for method invocation as well as property access, and the two can be intermixed. For example:
will be translated properly.
Also the macro makes the arguments to method and function calls null-safe as well. So in the case of:
you don’t have to worry if d
or e
would be null
.
Custom default for ?
For the ?
macro, you can also provide a custom default instead of null
, by passing it in as the second parameter. For example
?? macro
There’s also a ??
(null coalesce operator) which is used to select the first non-null value from a var-args list of expressions.
The null-safe coalesce operator also rewrites each arg so that it’s null safe. So you can pass in a.b.c
as an expression without worrying if a
or b
are null
. To be more explicit, the ??
macro would translate ??(a.b.c,a2.b.c)(default)
into
Compared to the ?
macro, the ??
macro checks that the entire expression is not null
, whereas the ?
macro would just check that the preceding elements (e.g. a
and b
in a.b.c
) aren’t null
before returning the default value.
Efficient null-checks
The macro is also smart about what it checks for null, so anything that is <: AnyVal
will not be checked for null. For example
Would be translated to:
Performance
Here’s the result of running the included jmh benchmarks:
Data in tabular form
You can find the source code for the JMH benchmarks here. If you want to run the benchmarks yourself, just run sbt bench
, or sbt quick-bench
for a shorter run.
Conclusion
These benchmarks compare all of the known ways (or at least the ways that I know of) to handle null-safety in scala. It demonstrates that the explicit null-safety is the highest performing and that the ScalaNullSafe macro has equivalent performance.
In the next section we’ll examine how the usage of null
will evolve in the next major version of Scala, Scala 3, AKA Dotty.