-1

Why following code:

/.../
.onFailure(exc ->
                        Match(exc).of(
                                Case($(instanceOf(ServerSOAPFaultException.class)), handleServerSOAPFaultException(exc)),
                                Case($(instanceOf(Exception.class)), handleDefaultException(exc))
                        ))
                .getOrElseGet(exc -> false);

invoke first case when exception is RuntimeException. RuntimeException is not an instance of ServerSOAPFaultException.

PoorGuy
  • 29
  • 8

1 Answers1

0

This is a Java problem, not a Vavr specific one: in Java, if you write foo(a, b) then both a and b will be evaluated before foo(a, b) is evaluated (called "eager").

So to evaluate the Match().of() it must evaluate all Case(). To evaluate each Case() it must evaluate handleServerSOAPFaultException(exc).

If you want to evaluate handleServerSOAPFaultException(exc) only when the case matches, you need to make it a lazy call, i.e. a function, i.e. a Supplier:

/.../
.onFailure(exc ->
                        Match(exc).of(
                                Case($(instanceOf(ServerSOAPFaultException.class)), () -> handleServerSOAPFaultException(exc)),
                                Case($(instanceOf(Exception.class)), () -> handleDefaultException(exc))
                        ))
                .getOrElseGet(exc -> false);
Sir4ur0n
  • 1,753
  • 1
  • 13
  • 24