Switch on several values

Clément BOUDEREAU cboudereau at gmail.com
Wed Sep 27 16:57:56 UTC 2023


Here is the infinite/fixed code working in rust for conciseness :
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=90bc78702650e8bd12af9efdd71d969a

On Wed, Sep 27, 2023, 17:54 Clément BOUDEREAU <cboudereau at gmail.com> wrote:

> Hi Remi,
>
> Thank you for your feedback, this is exactly what I wanted to do.
>
> Here is a tennis kata I made using pattern matching only:
>
> https://gist.github.com/cboudereau/1f3ea7acd6d6712746c6d9f0e258deff#file-tennis-rs-L50
> https://gist.github.com/cboudereau/ff891382a345ca81e8f4c6c836d9837b
>
> You can also match higher tuple cardinality.
>
> regards
>
> On Wed, Sep 27, 2023 at 4:04 PM Remi Forax <forax at univ-mlv.fr> wrote:
>
>> 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
>>
>
>
> --
> C.BOUDEREAU
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230927/67fd5e51/attachment-0001.htm>


More information about the amber-dev mailing list