[英]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.我正在使用 Vavr 在 vavr 元组上进行模式匹配,但我似乎无法让模式匹配在元组中工作。
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?我在这里做错了什么?
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")
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.