nidula 0.1.1 nidula: ^0.1.1 copied to clipboard
A lightweight Dart library for Rust-like Option/Result types. Supports exhaustive pattern matching and compile-time safe, chainable None/Err propagation.
import 'package:nidula/nidula.dart';
Result<double, String> example(Result<double, String> r) {
return Result.syncErrPropagationScope<double, String>((t) {
r = Ok(r.unwrapOrPropagateErr(t) / 2);
return Ok(r.unwrapOrPropagateErr(t));
});
}
void main() {
print('#1: ${example(Err('s'))}'); // #1: Err(s)
print('#2: ${example(Ok(14))}'); // #2: Ok(7.0)
print(Result.syncErrPropagationScope<I, String>((t) {
return Ok(
Ok<I, String>(I())
.unwrapOrPropagateErr(t)
.make()
.unwrapOrPropagateErr(t)
.make()
.unwrapOrPropagateErr(t),
);
}));
}
class I {
Result<I, String> make() {
return Ok(I());
}
}