functional-reactive
Made in the European Union

Result<T>

A Success/Failure container with map, flatMap and async combinators.

Result<T> is the central error-handling primitive of functional-reactive. Think of it as Optional<T> with a reason for the absence.

Result<User> user = repository.findById(id);

user.ifPresentOrElse(
    u   -> log.info("Found {}", u),
    err -> log.warn("Lookup failed: {}", err)
);

Construction

MethodDescription
Result.success(t)Wraps a non-null value.
Result.failure(reason)Marks the absence of a value with a message.
Result.ofNullable(t)Success if non-null, otherwise failure.
Result.fromOptional(opt)Bridge from java.util.Optional.

Transformations

  • map(Function) โ€” transform the value if present.
  • flatMap(Function<T, Result<U>>) โ€” chain another Result-returning operation.
  • or(Supplier) โ€” fall back to another Result.

Combinators

  • thenCombine(value, BiFunction) โ€” combine with a plain value.
  • thenCombineAsync(value, BiFunction) โ€” same, but asynchronous.

Terminal operations

  • ifPresent(Consumer) / ifAbsent(Runnable) / ifFailed(Consumer<String>)
  • ifPresentOrElse(Consumer, Consumer<String>)
  • stream() โ€” zero-or-one element stream.
  • toOptional() โ€” back to java.util.Optional.
When to reach for Result
Use Result whenever absence has a reason the caller cares about โ€” failed parsing, missing config, lookups that may fail. Reach for Optional when absence is the entire information.