I disagree: as soon as a case matches, it stops evaluating other cases, assuming you write your cases in lazy mode (e.g. with Predicates and Suppliers). The problem comes from Java being eager by default on its arguments evaluation, this has nothing to do with Vavr.
Here's a counter-example of what you claim. Please note:
- the matchers are lazy (written with Predicate)
- the values are lazy (written with Supplier)
.
public class Main {
public static void main(String[] args) {
var result = Match("foo").of(
Case($(choice("one")), () -> result("1")),
Case($(choice("two")), () -> result("2"))
);
System.out.println(result);
}
static Predicate<String> choice(String choice) {
return string -> {
System.out.println("Inside predicate " + choice);
return true;
};
}
static String result(String result) {
System.out.println("Inside result " + result);
return result;
}
}
When executed, this yields:
Inside predicate one
Inside result 1
1
Please note that neither the second predicate nor the second result were ever evaluated.