> De: "Brian Goetz" <[email protected]>
> À: "Guy Steele" <[email protected]>
> Cc: "Remi Forax" <[email protected]>, "amber-spec-experts"
> <[email protected]>, "John Rose" <[email protected]>
> Envoyé: Vendredi 7 Août 2020 16:48:13
> Objet: Re: Nullable switch
>> Okay, so it would seem that we need two keywords (or other syntax) for use in
>> patterns; I will temporarily call them “anything-but-null” and
>> “anything-including-null”.
> Not necessarily; the approach we've been driving towards has no (new)
> keywords,
> and no _explicit_ consideration of nullability. There's just type patterns,
> but
> their semantics take into account whether or not the type pattern "covers" the
> target type. This is subtle, I grant, and I can see where people would get
> confused, but it is far more compositional and less ad-hoc.
> Ignoring the epicyclical* distastefulness of the "any x" idea, I think the the
> syntax issues are a bit of a red herring -- the issue is structural. Under
> Remi's proposal, there is simply _no_ way to write a switch where any number
> of
> cases covers "anything including null", because the switch will throw before
> you get there:
> switch (x) {
> case String s:
> case Object o:
> }
> would throw on NPE (as switches do today) before any cases are considered,
> whether you say "var" or "any" or "Object."
That is not true.
You're right that the switch above will generate a NPE as the switches do today
because under the rules i propose, there is no case that accept null.
But if you add an any case (or a null case), then the switch will accept null,
by example, the switch below accept null.
switch (x) {
case String s:
case any o:
}
As Guy said, i'm proposing to have two different cases, one “anything-but-null”
and one “anything-including-null” instead of relying on the non-local property
of totality.
You can re-read my email from the 6th of August for the rules allowing a switch
to accept null and more examples.
regards,
Rémi