The case for no case
forax at univ-mlv.fr
forax at univ-mlv.fr
Sat Jun 6 14:19:18 UTC 2020
Let's try again with a different camera angle.
If i want to write the equals of a record Point, using instanceof, I can write
record Point(int x, int y) {
public boolean equals(Object o) {
return o instanceof Point(x, y);
}
}
and using a switch, I will write
record Point(int x, int y) {
public boolean equals(Object o) {
return switch(o) {
case Point(x, y) -> true;
default -> false;
}
}
the issue with 'case Point(x,y)' is that for most people, it's like 'case "foo"', but it's not case "foo" uses the equals() while 'case Point(x,y)' is using a deconstructor, something which is more clear with 'instanceof Point(x, y)',
that why i had propose to use 'instanceof Point(x, y)' inside the switch.
But as Brian said, it's ugly with inner patterns, and as you said, pattern matching switch will be heterogeneous anyway.
I don't have the solution, maybe this problem is just because we are introducing new patterns but in 10 years, people will be used too.
Another idea is to reserve "case" for constants and do not allow "case" in front of the type pattern but i'm not sure it makes a real difference.
Rémi
> De: "Kevin Bourrillion" <kevinb at google.com>
> À: "John Rose" <john.r.rose at oracle.com>
> Cc: "Remi Forax" <forax at univ-mlv.fr>, "amber-spec-experts"
> <amber-spec-experts at openjdk.java.net>
> Envoyé: Vendredi 5 Juin 2020 04:20:22
> Objet: Re: The case for no case
> For what it's worth, I don't think that users want to discriminate finely
> between different kinds of patterns. Some just happen to only match one thing.
> Even in the math world, when reading a proof by exhaustion, often the cases are
> heterogeneous, and I don't think it really bothers anyone. You just want to be
> able to see that everything is covered by the time you're done.
> On Thu, Jun 4, 2020 at 5:14 PM John Rose < [ mailto:john.r.rose at oracle.com |
> john.r.rose at oracle.com ] > wrote:
>> On Jun 4, 2020, at 2:17 AM, Remi Forax < [ mailto:forax at univ-mlv.fr |
>> forax at univ-mlv.fr ] > wrote:
>>> One simple solution is to use different keyword for different kind of patterns,
>>> by example, "case" for a constant pattern, "instanceof" for the type pattern,
>>> "default" the default pattern, etc
>> That strikes me as making the job of mechanical parsing easier, and the job of
>> *reading* harder.
>> You’d replace a uniform left column of case/case/case with a heterogeneous mix
>> of keywords.
>> And most of the time you wouldn’t care which is which.
> --
> Kevin Bourrillion | Java Librarian | Google, Inc. | [ mailto:kevinb at google.com |
> kevinb at google.com ]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20200606/af3134fc/attachment.htm>
More information about the amber-spec-experts
mailing list