Result example in java

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Algebraic Result type in Java 15+, return type for oprations that can fail

License

mverleg/java-result

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Result is a value that can be either Ok or Err , to signal whether an operation succeeded or failed. Each variant can contain data, e.g. Result contains a User if ok, and ErrMsg when it fails.

It can be used as a return value from functions to indicate if they succeeded or failed, similar to Optional , but with data about why it failed.

Java-result is feature-complete and can be used in Java 15+. It has extensive unit test coverage, but limited real-world testing.

Functions can return Result to indicate whether they failed and why:

@Nonnull public static ResultInteger, DivError> divide(@Nullable Integer numerator, @Nullable Integer divisor) < if (null == numerator) < return Result.err(DivError.NUMERATOR_NULL); > if (null == divisor) < return Result.err(DivError.DIVISOR_NULL); > if (0 == divisor) < return Result.err(DivError.DIVISOR_ZERO); > return Result.ok(numerator / divisor); >
ResultInteger, DivError> costPerPerson = divide(cost, people); if (costPerPerson.isOk()) < sendMessage("You need to pay " + costPerPerson.get()); >
divide(cost, people) .ifOk(costPerPerson -> sendMessage("You need to pay " + costPerPerson));
divide(9, 0).map(result -> result / 2) // Err(DIVISOR_ZERO)
divide(8, 2).flatMap(res -> divide(res, 2)) // Ok(2)
divide(8, 0).withoutErr().orElse(1) // Ok(1) divide(8, 0).withoutErr().orElseGet(() -> calculateFallback()) // calculateFallback() computed only if failed divide(8, 0).withoutErr().recover(err -> calculateFallback2(err)) // calculateFallback2(err) computed only if failed
ResultString, Exception> userName = Result.attempt(() -> findUserName());
divide(8, divisor).getOrThrow("if you see this, sorry. ") // throws
ResultString, Exception> userNameResult = Result.attempt(() -> findUserName()); if (userNameResult instanceof OkString, Exception> userName) < return doSomethingWithUsername(userName.get()); > else < return userNameResult.adaptOk(); // Changes the ok type, which is safe because this is a failed Tesult >
ListResultInteger, String>> list = List.of(ok(1), ok(2), err("problem"), ok(4)); ListInteger> successesOnly = list.stream() .flatMap(Result::stream).toList(); // [1, 2, 4]
ListResultInteger, String>> list = List.of(ok(1), ok(2), err("problem"), ok(4)); ResultListInteger>, String> listResult = Result.transpose(list); // Err(problem)
ResultListInteger>, DivError> streamResult = Stream.of(2, 1, 0, -1, -2) .map(nr -> divide(10, nr)) .collect(ResultCollector.toList()); // Err(DIVISOR_ZERO)

Java-result is available on Central: nl.markv.result.

dependency> groupId>nl.markvgroupId> artifactId>resultartifactId> version>1.1.0version> dependency>

For Java 15/16 this uses preview features. Java 14 and below are not supported.

For Java 15+, add this dependency:

implementation 'nl.markv:result:1.1.0'

For Java 15/16 this uses preview features. Java 14 and below are not supported.

With sealed interfaces in Java 15 (preview feature), it finally has decent support for sum types — algebraic types that can have one out of a finite set of values. They are sometimes called unions or composite types.

You can think of it as an enhanced enum , where each variant is a different subtype, instead of single instance. Each variant can have a different structure, and can have any number of instances.

Many languages that support sum types, like Kotlin, Haskell, Rust, Swift, C++ or others, have some kind of type that indicates one of two options — for example, success or failure. Java has Optional , but that cannot contain an error value.

Result is a popular example of such types, which has two variants: one for success and one for failure. It can be used for error handling.

If you are familiar with monads, Result is a monad with unit operations ok / err , bind operation map / mapErr , and a flattening operation flatMap / flatMapErr or flatten .

About

Algebraic Result type in Java 15+, return type for oprations that can fail

Источник

Читайте также:  Import for java collections
Оцените статью