1

I'm using Vavr to do pattern matching on a vavr-tuple but I can't seem to do get the pattern matching to work in tuple.

Here is my code


Tuple2 test = Tuple.of("foo", "bar");

Match(test)
    .of(
        Case($(API.Tuple("foo",$())), "baz")
    );

Here is the error message I get

io.vavr.MatchError: type: io.vavr.Tuple2, value: (foo, bar)

    at io.vavr.API$Match.of(API.java:5095)....

I expect the wild card to ignore what the second element is in the tuple.

This way of using the $() wild card seems to work though, so it seems like I can't use it within a tuple

Tuple2 test = Tuple.of("foo", "bar");
Match(test)
    .of(
        Case($(), "baz")
    );

What am I doing wrong here?

1 Answers1

0

You could use the predicate version of the wildcard

$(java.util.function.Predicate<? super T> predicate)

and then compare the first value of your tuple.

final var test = Tuple.of("foo", "bar");
Match(test).of(
    Case($(t -> t._1().equals("foo")), "baz"),
    Case($(), "default")
);
Miracoly
  • 63
  • 7