https://github.com/kyle-silver/result-type-jvm
An ergonomic, Rust-inspired result type for JVM languages
https://github.com/kyle-silver/result-type-jvm
algebraic-data-types error-handling java
Last synced: 6 months ago
JSON representation
An ergonomic, Rust-inspired result type for JVM languages
- Host: GitHub
- URL: https://github.com/kyle-silver/result-type-jvm
- Owner: kyle-silver
- License: mit
- Created: 2022-01-01T01:26:48.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-07-03T19:54:08.000Z (almost 4 years ago)
- Last Synced: 2025-07-25T20:52:37.446Z (11 months ago)
- Topics: algebraic-data-types, error-handling, java
- Language: Java
- Homepage: https://kyle-silver.github.io/result-type-jvm/dev/kylesilver/result/Result.html
- Size: 170 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# A Result Type for the Rest of Us
This package contains a small, composable Result type heavily inspired by Rust. It provides ergonomic and robust error handling and leverages Java's type system to make uncaught runtime exceptions a thing of the past.
## Installation
This package can be found on [Maven Central](https://search.maven.org/artifact/dev.kylesilver/result-type-jvm). For POM files, add the following:
```xml
dev.kylesilver
result-type-jvm
0.1.2
```
For Gradle projects, use:
```groovy
implementation 'dev.kylesilver:result-type-jvm:0.1.2'
```
## Usage
A `Result` type indicates that a value is either a success `Ok` or an error `Err`. Before accessing the value inside, you must inspect the `Result` to determine which of the two possible outcomes have occurred.
Instead of throwing an exception, you can return an error value.
```java
// indicates the value was successful
Result ok = Result.ok(1);
// indicates there was an error
Result err = Result.err("something went wrong!");
```
If you want to get the value out and throw an exception if there's an error, you can do so with `unwrap()`
```java
int value = result.unwrap();
```
You can also substitute a default value in the event of an error
```java
int value = result.err().orElse(0);
```
And you can also apply stream-style maps which are only applied to the value if it is `Ok`
```java
// the map is applied to Ok values
Result result1 = Result.ok(5);
String result = result1.map(x -> Integer.toString(x)).unwrap();
// but not to errors
Result result2 = Result.err("whoops!");
result2.map(x -> Integer.toString(x)).isErr(); // true
```
You can use `match` to apply more complex manipulations to the result of an operation.
```java
Result result = Result.ok(5);
int computed = result.match(
ok -> ok + 1,
err -> {
log.warn("there was an error!");
return 0;
}
);
```
There's a lot more that you can do with Result types, check out the [docs](https://kyle-silver.github.io/result-type-jvm/dev/kylesilver/result/Result.html) for more details.