From jens at lidestrom.se Sat Jul 25 19:51:37 2020 From: jens at lidestrom.se (=?UTF-8?Q?Jens_Lidestr=c3=b6m?=) Date: Sat, 25 Jul 2020 21:51:37 +0200 Subject: Observation about nulls in type patterns Message-ID: <99e2350d-8c38-9d72-22ed-ce5d4cff0849@lidestrom.se> 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