<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<font size="4"><font face="monospace">[ dropping amber-dev ]<br>
<br>
Yes, we've discussed this before. A switch could have multiple
selector values:<br>
<br>
switch (a, b)<br>
<br>
and cases could be structured similarly:<br>
<br>
case (P, Q):<br>
<br>
All of this isn't hard. The real question is, does it result in
better or worse code? My sense is that it look really pretty in
the simple examples, but as the number of selectors and the size
of the patterns increases, it is likely to become an unreadable
soup. So that's a concern.<br>
<br>
As an example of a "the simple cases are very pretty", I give
you a world-class FizzBuzz:<br>
<br>
Function<Integer, String> fizzbuzz = <br>
x -> switch (x % 3, x % 5) { <br>
case (0, 0) -> "FizzBuzz";<br>
case (0, _) -> "Fizz";<br>
case (_, 0) -> "Buzz";<br>
default -> Integer.toString(x);<br>
};<br>
<br>
(I might have gotten my Fizz and Buzz backwards, I didn't bother
to look it up.) <br>
<br>
<sarcasm><br>
I see the appeal in "Java is the #1 language for writing
FizzBuzz", but I am not sure this is the stewardship rubric we
are looking for :)<br>
</font></font><font size="4"><font face="monospace"><font size="4"><font face="monospace"></sarcasm><br>
<br>
</font></font>Another problem (and this one is of our own
making) is that comma already means something in cases. So <br>
<br>
case 0, 1<br>
<br>
means something subtly different from<br>
<br>
case (0, 1)<br>
<br>
which is not particularly nice. Yes, the type checker will
disambiguate for you, but we are not used to both `X` and `(X)`
being valid in the same context but meaning different things.
(Worse, when you combine these where you have multiple tuple
patterns on one case, it's even more case soup.) <br>
<br>
So I put this one in the category of "simple enough to specify
and implement, has obvious motivating use cases, but not sure it
actually improves the language."<br>
<br>
<br>
</font></font><br>
<div class="moz-cite-prefix">On 9/27/2023 10:03 AM, Remi Forax
wrote:<br>
</div>
<blockquote type="cite" cite="mid:525180955.6049957.1695823398327.JavaMail.zimbra@univ-eiffel.fr">
<pre class="moz-quote-pre" wrap="">Hi recently Clément BOUDEREAU has reported a bug on amber-dev and unrelated to that bug,
taking a look to the code I've noticed this
int compareTo(final Value<T> o) {
return switch (new Tuple<>(this, o)) {
case Tuple<Value<T>, Value<T>>(Value.Infinite<T> _, Value.Infinite<T> _) -> 0;
case Tuple<Value<T>, Value<T>>(Value.Infinite<T> _, Value.Fixed<T> _) -> 1;
case Tuple<Value<T>, Value<T>>(Value.Fixed<T> _, Value.Infinite<T> _) -> -1;
case Tuple<Value<T>, Value<T>>(Value.Fixed<T> fst, Value.Fixed<T> snd) ->
fst.value.compareTo(snd.value);
};
}
Here what Clément want is to match two values (here, "this" and "o") but the only way to do that is to wrap them into a pair (here named Tuple),
Should we not provide a way to match several values natively ?
Something like
int compareTo(final Value<T> o) {
return switch (this, o) {
case (Value.Infinite<T> _, Value.Infinite<T> _) -> 0;
case (Value.Infinite<T> _, Value.Fixed<T> _) -> 1;
case (Value.Fixed<T> _, Value.Infinite<T> _) -> -1;
case (Value.Fixed<T> fst, Value.Fixed<T> snd) ->
fst.value.compareTo(snd.value);
};
}
regards,
Rémi
</pre>
</blockquote>
<br>
</body>
</html>