okay 0.6.0
okay: ^0.6.0 copied to clipboard
Typesafe error-handling for dart . An implementation of rust's `Result` type in dart.
okay #
Typesafe, intuitive error-handling for dart . An implementation of rust's Result type in dart.
Result<T, E> is a type used for returning and propagating errors. It is a type with the variants, ok(T), representing success and containing a value, and err(E), representing error and containing an error value.
Note: This package was heavily inspired by rustlang's result type.
Installation #
In the dependencies: section of your pubspec.yaml, add the following line:
dependencies:
okay: <latest_version>
Basic Usage #
import 'package:okay/okay.dart';
class FallibleOpFailure {}
Result<String, FallibleOpFailure> fallibleOp() {
if (true) {
return ok('Very good string');
} else {
return err(FallibleOpFailure());
}
}
void useString(String value) {
//
}
final result = fallibleOp();
final goodString = result.inspect((value) {
print('Success with value: $value');
}).inspectErr((error) {
print('Failure with error: $error');
}).unwrapOr('Fallback string');
useString(goodString);
Methods and Getters Overview #
Extracting Contained Values #
expectreturns contained value if ok, throws an exception with provided message if err.unwrapreturns contained value ifok, throws an exception iferr.unwrapOrreturns contained value ifok, returns the provided fallback value iferr.unwrapOrElsereturns contained value ifok, returns the result of function provided iferr(function takes in the contained error type and returns a value type).
Inspect, use the contained values without consuming the result #
inspectCalls the provided closure with the contained value (ifok) without consuming the resultinspectErrCalls the provided closure with the contained error (iferr) without consuming the result.
Querying the variant #
isOkReturns true if ofokvariant, false if not.isOkAndReturns true if the result isokand the contained value matches the provided predicate function, otherwise returns false.isErrReturns true if oferrvariant, false if not.isErrAndReturns true if the result iserrand the contained value matches the provided predicate function, otherwise returns false.
Adapters #
okconverts aResult<T, E>to aT?, i.e, ifok, T, iferr, null.
Transforming contained values #
mapOrElseConverts aResult<T, E>to aUgiven aU errMap(E)and aU okMap(T)mapOrConverts aResult<T, E>to aU, given aU fallbackandU okMap(T)mapConverts aResult<T, E>toResult<U, E>by applying the provided function if to contained value if ok, or returning the original error if err.
OkOrErr on Result<T, T> #
okOrErrReturns value ifokor error iferr(as the most precise same typeT).
Compair contained ok or err values_ #
containsReturns true if the result is anokvalue containing the given value, otherwise returns falsecontainsErrReturns true if the result is anerrvalue containing the given value, otherwise returns false
Boolean operators #
These methods treat the Result as a boolean value, where the ok variant is acts like true and err acts like false.
The andandor take another Result as input, and produce Result as output. The and method can produce a Result<U, E> value having a different inner type U than Result<T, E>. The or method can produce a Result<T, F> value having a different error type F than Result<T, E>.
| method | this | input | output |
|---|---|---|---|
and |
err(e) |
-- | err(e) |
and |
ok(x) |
err(d) |
err(d) |
and |
ok(x) |
ok(y) |
ok(y) |
or |
err(e) |
err(d) |
err(d) |
or |
err(e) |
ok(y) |
ok(y) |
or |
ok(x) |
-- | ok(x) |
The andThen and orElse methods take a function as input, and only evaluate the function when they need to produce a new value. The andThen method can produce a Result<U, E> value having a different inner type U than Result<T, E>. The orElse method can produce a Result<T, F> value having a different error type F than Result<T, E>.
| method | this | function input | function result | output |
|---|---|---|---|---|
andThen |
err(e) |
-- | -- | err(e) |
andThen |
ok(x) |
x |
err(d) |
err(d) |
andThen |
ok(x) |
x |
ok(y) |
ok(y) |
orElse |
err(e) |
e |
err(d) |
err(d) |
orElse |
err(e) |
e |
ok(y) |
ok(y) |
orElse |
ok(x) |
-- | -- | ok(x) |