Interface Result.Binder<E>

Enclosing interface:
Result<E,T>
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public static interface Result.Binder<E>
The unwrapping handle passed to Result.binding(Function).

Calling on(Result) inside a binding block either returns the success value or aborts the whole block at the first Result.Err encountered.

The error type is consumed covariantly: a block typed Binder<BankError> accepts steps returning Result<NoSuchAccount, …>, Result<WrongPin, …>, and so on, where each subtype implements BankError. No per-step mapErr is needed; the binding block unifies the subtypes into the block's E automatically. See ADR-11.

  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    ensure(boolean condition, Supplier<? extends E> error)
    Assert a condition with no value to bind: a guard, a permission check, an invariant.
    <T> T
    on(Result<? extends E, T> result)
    Unwrap a Result inside a binding block: returns the value if Result.Ok, otherwise short-circuits the enclosing block, which then evaluates to that Result.Err.
    default <T> T
    on(Optional<? extends T> maybe, Supplier<? extends E> ifEmpty)
    Bind an Optional inside a binding block: a present value is returned, an empty Optional short-circuits the block with ifEmpty.get().
  • Method Details

    • on

      <T> T on(Result<? extends E, T> result)
      Unwrap a Result inside a binding block: returns the value if Result.Ok, otherwise short-circuits the enclosing block, which then evaluates to that Result.Err. Only legal while the surrounding binding call is on the stack.

      The error is consumed covariantly (? extends E), so steps that return a subtype of the block's error are accepted without mapErr.

    • on

      default <T> T on(Optional<? extends T> maybe, Supplier<? extends E> ifEmpty)
      Bind an Optional inside a binding block: a present value is returned, an empty Optional short-circuits the block with ifEmpty.get(). Lets Optional- and Result-returning calls be sequenced together without bridging each one by hand.
      var candidates = bind.on(NonEmptyList.fromList(xs), AppError.NoCandidates::new);
      
    • ensure

      default void ensure(boolean condition, Supplier<? extends E> error)
      Assert a condition with no value to bind: a guard, a permission check, an invariant. false short-circuits the enclosing block with the supplied error, exactly like binding an Err; true is a no-op.