https://github.com/kellpossible/rustytypes
Java types inspired by the Rust programming language
https://github.com/kellpossible/rustytypes
Last synced: 9 months ago
JSON representation
Java types inspired by the Rust programming language
- Host: GitHub
- URL: https://github.com/kellpossible/rustytypes
- Owner: kellpossible
- License: mit
- Created: 2018-09-13T15:38:52.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-13T16:11:27.000Z (over 7 years ago)
- Last Synced: 2025-03-24T03:47:09.131Z (about 1 year ago)
- Language: Java
- Size: 52.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RustyTypes for Java
This project is a collection of Java types which are inspired by the Rust programming language.
## Types
### [Result](src/main/java/org/kellpossible/rustytypes/Result.java)
This type is inspired by [Rust's Result enum](https://doc.rust-lang.org/std/result/).
A `Result` method can be created by using its static constructor methods `ofValue()` or `ofError()` to create a result which contains either a result value or an error.
```java
Result resultWithValue = Result.ofValue(value);
Result resultWithError = Result.ofError(error);
```
The only way to access the value inside the `Result` is to use the `handle()` method. This method has two overloaded implementations. One makes use of lambdas, and the other uses the `ResultHandler` interface along with its two methods. This method forces consumers of the `Result` to consider the implications of either a value being present or an error being present.
```java
result.handle(
value -> {
// handle what happens if the result contains a value
},
error -> {
// handle what happens if the result contains an error
}
);
```