Skip to content

Instantly share code, notes, and snippets.

@Kshitij-Dhakal
Created July 19, 2023 08:22
Show Gist options
  • Save Kshitij-Dhakal/330c708ea1fbd091ac76abde8a144c9f to your computer and use it in GitHub Desktop.
Save Kshitij-Dhakal/330c708ea1fbd091ac76abde8a144c9f to your computer and use it in GitHub Desktop.
Rust Result Emulation
public class Result<T, E> {
T t;
E e;
private Result() {
}
public static <T, E> Result<T, E> ok(T ok) {
var teResult = new Result<T, E>();
teResult.t = ok;
return teResult;
}
public static <T, E> Result<T, E> err(E err) {
var teResult = new Result<T, E>();
teResult.e = err;
return teResult;
}
public <U> U match(Function<T, U> ok, Function<E, U> err) {
if (ok != null) {
return ok.apply(this.t);
} else {
return err.apply(this.e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment