new Vavr user here. I am trying to pattern match a tuple of options to execute a statements if both of them are Some, in Scala I would have done this with:
val maybeThis: Option[String] = ???
val maybeThat: Option[String] = ???
(maybeThis, maybeThat) match {
case (Some(dis), Some(that)) => ???
case _ => ???
}
In Java, I am trying this approach:
Tuple2<Option<String>, Option<String>> tuple = new Tuple2<>(Option.of(...), Option.of(...));
return Match(tuple).of(
Case($Tuple2($Some($(instanceOf(String.class))), $Some($(instanceOf(String.class)))),
(someThis, someThat) -> methodWhichEatsTwoStrings(someThis.get(), someThat.get())),
Case($(), t -> run(() -> {
throw new NullPointerException(...);
})));
However, with this implementation the compiler complains that it was expecting Some<Object>
instead of Some<String>
, same error happens if I omit the $(instanceOf(String.class)
in the pattern.
I am moderately sure this is merely a problem of proper syntax, yet I am struggling to find the correct documentation. Where am I wrong? Thanks
There's a vavr API construct called for comprehension that tries to mimic Scala's for comprehensions to the extent that it's possible to do in Java. With that construct you could solve your problem quite elegantly. See the For overload for handling two Options for more details. Here's an example code snippet:
Of course, you could use
Option
wrapping values of different types foroption1
andoption2
, or combine multiple options, not just two. You could also use a different type for the return value of theyield
function as well. I usedString
everywhere for the sake of simplicity and to conform to your original example.I would like to add that I would try to avoid throwing
NullPointerException
in case one or both of the options are empty. Maybe try to use another vavr data type likeEither
to represent such an error case?