Observation about nulls in type patterns
Jens Lideström
jens at lidestrom.se
Sat Jul 25 19:51:37 UTC 2020
Brian Goetz posted a write-up of the feature Types patterns in switch in
amber-spec-experts on Jun 24:
https://mail.openjdk.java.net/pipermail/amber-spec-experts/2020-June/002235.html
I'd like to contribute an observation about the consequences of the
proposed mechanism for handling null values in switches:
If I understand the proposal correctly the behaviour of null values in a
switch will depend on the type of the expression being switch upon. This
is potentially non-local information, for example when the expression is a
field access or a method call.
To understand the behaviour of null values in a switch readers will have
to examine the source of the expression being switch upon. Also, if the
type of a method or a field is being narrowed or widened that change might
silently affect the behaviour of existing switches in other parts of the code.
Example:
switch (o.get1().get2()) {
case Integer i: ...
case Number n: ...
}
If get2 is some method that is declared to return Number then the second
pattern is total and will accept null; if get2 is declared to return
Object pattern the IS total and WILL accept null.
The expert group is probably aware of this fact already but I think it
deserves to be noted explicitly.
Best regards,
Jens Lideström
Random Passer-by
More information about the amber-spec-comments
mailing list