Switch on several values

Remi Forax forax at univ-mlv.fr
Wed Sep 27 14:03:18 UTC 2023


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


More information about the amber-dev mailing list