floyergilmour
floyergilmour

Reputation: 125

Java Vavr pattern matching on tuple using wild card

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?

Upvotes: 1

Views: 293

Answers (1)

miracoly
miracoly

Reputation: 123

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")
);

Upvotes: 0

Related Questions