Next up for patterns: type patterns in switch
forax at univ-mlv.fr
forax at univ-mlv.fr
Thu Aug 13 12:19:42 UTC 2020
> De: "Guy Steele" <guy.steele at oracle.com>
> À: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "Brian Goetz" <brian.goetz at oracle.com>, "John Rose"
> <john.r.rose at oracle.com>, "amber-spec-experts"
> <amber-spec-experts at openjdk.java.net>
> Envoyé: Jeudi 13 Août 2020 04:46:31
> Objet: Re: Next up for patterns: type patterns in switch
>> On Aug 12, 2020, at 3:57 PM, [ mailto:forax at univ-mlv.fr | forax at univ-mlv.fr ]
>> wrote:
>> . . .
>> I agree destructuring is just as important as conditionality and those two
>> things should be orthogonal.
>> But i still think having a keyword to signal that a pattern (not a case) is
>> total is better than letting people guess.
> Yes, and here is the example that convinced me that one needs to be able to mark
> patterns as total, not just cases:
> (Assume for the following example that any pattern may be preceded by “default”,
> that the only implication of “default” is that you get a static error if the
> pattern it precedes is not total, and that we can abbreviate “case default” as
> simply “default”.)
> record Box<T>(T t) { }
> record Bag<T>(T t) { }
> record Pair<T,U>(T t, U u) { }
> Triple<Box<Frog>, Bag<Object>> p;
> switch (x) {
> case Pair(Box(Tadpole t), Bag(String s)): …
> case Pair(Box(Tadpole t), Bag(default Object o)): …
> case Pair(Box(default Frog f), Bag(String s)): …
> default Pair(Box(Frog f), Bag(Object o)): …
> }
> I think there is some charm to this.
For the last use of default,
default Pair(Box(Frog f), Bag(Object o)): …
I wonder if we find it natural only because we are used to use the keyword "default" inside a switch,
using a different keyword, by example"total",
total Pair(Box(Frog f), Bag(Object o)): …
breaks the spell for me.
I think i prefer using "default" (or any other keyword) only where it makes sense and doesn't allow "default" to be propagated.
so
default Pair p: ...
is ok but
default Pair(Box(Frog f), Bag(Object o)): …
should be written
case Pair(Box(Frog f), Bag(default Object o)): …
> To be clear, the intent of this variant proposal is that _any_ pattern may have
> “default” in front of it, but I suspect that in practice its primary use will
> be within “switch” (or at least within some sort of conditional context), which
> is what justifies the use of the keyword “default”. Certainly
> default Pair(T x, U y) = expr;
> is not especially useful, because if Pair(T x, U y) is not total on the type of
> expr we presumably get a static error from the declaration even without the
> keyword “default” present. Possibly someone might write
> if (x instanceof FrogBox(Toad t)) …
> else if (x instanceof FrogBox(Tadpole tp)) …
> else if (x instanceof default FrogBox(Frog f)) ...
> or
> if (x instanceof FrogBox(Toad t)) …
> else if (x instanceof FrogBox(Tadpole tp)) …
> else if (x instanceof FrogBox(default Frog f)) ...
> and either of those might actually be useful (in fact, I believe they are
> equivalent but differ in stylistic emphasis), but then that someone should
> remember that instanceof is never true when x is null (which may be what is
> wanted in this specific code).
> So “default” is in practice used much like “any” that Rémi earlier discussed,
> but the theory is that “default” does not dictate null-acceptance; rather,
> null-acceptance depends on pattern totality (in the manner Brian has argued
> for), and “default” is a way for the programmer to ensure pattern totality.
yes !
Rémi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20200813/e0cb88a1/attachment.htm>
More information about the amber-spec-experts
mailing list