Patterns and nulls
Brian Goetz
brian.goetz at oracle.com
Wed Mar 14 21:47:15 UTC 2018
> If records were changed to not allow null, then `new Box(null)` would
> not be possible. Would that not change the line of reasoning here?
No. Pattern matching is not just for records, it's just that (a) like
other conveniences, records come with deconstruction patterns free out
of the box, and (b) records are likely to be the first construct against
which will be able to pattern match. But over time, we'll extend
pattern matching to arbitrary classes and more.
Even if it did simplify pattern matching, it seems like an unreasonable
restriction on records. Like it or not, nulls are part of Java's data
model. (I understand the desire to not let _new_ constructs be polluted
by old mistakes, but its a siren song. Everything is connected to
everything else. It's one more case where you can't refactor a class to
a record. And having complex rules of "you can have null in these
classes but not those" just makes things more complicated.)
And even if it were a reasonable restriction, it would further interact
poorly with mutable records. Mutable records could sport a no-arg
constructor, but then you'd need a sensible non-null default for every
field. There might be sensible defaults for Strings (""), Lists (empty
list), etc, but that's surely not true for all the things you might put
in a record.
More information about the amber-dev
mailing list