Records and null-values

Kasper Nielsen kasperni at
Sat Aug 24 05:57:18 UTC 2019


I was looking through a codebase trying to see where I could apply records.
And I realised that there was not a single place where I did not check for
null via requireNotNull() in the constructor. And looking at the use cases
for records I'm sure most that is the case for most people.

I know @Nullable annotations don't get much love, and it might have some
deeper implications in the language. But it would save a lot of boilerplate
if you where able to do something like:

record R(Object key, @Nullable Object value) {}

being equivalent to the constructor declaration

record R(Object key, @Nullable Object value) {
  R {
    this.key = requireNonNull(key, "key is nul");
    this.value = value;

It would probably also prevent a couple of NPEs somewhere.


More information about the amber-dev mailing list