nidula 1.1.0 copy "nidula: ^1.1.0" to clipboard
nidula: ^1.1.0 copied to clipboard

A Dart library for Rust-like Option/Result types. Offers a compile-time safe and chainable parallel to Rust's try operator for None/Err propagation.

example/nidula_example.dart

import 'dart:math';

import 'package:nidula/nidula.dart';

/// The following is a simple Err propagation example.
Result<double, String> divideBy2WithErrPropagation(Result<double, String> r) {
  return Result.tryScope<double, String>((et) {
    return Ok(r.try_(et) / 2);
  });
}

void main() {
  print('#1: ${divideBy2WithErrPropagation(Err('s'))}'); // #1: Err(s)
  print('#2: ${divideBy2WithErrPropagation(Ok(14))}'); // #2: Ok(7.0)

  // the next example uses error propagation: there is a 50% chance `make`
  // propagates an Err, and 50% that it returns an Ok.
  print(Result.tryScope<I, String>((et) {
    return Ok(
      Ok<I, String>(I()).try_(et).make().try_(et).make().try_(et),
    );
  })); // either `Err(unfortunate)` or `Ok(Instance of 'I')`
}

class I {
  Result<I, String> make() {
    if (Random().nextBool()) {
      return Ok(I());
    }
    return Err('unfortunate');
  }
}
7
likes
160
points
98
downloads

Publisher

verified publishermanuelplavsic.ch

Weekly Downloads

A Dart library for Rust-like Option/Result types. Offers a compile-time safe and chainable parallel to Rust's try operator for None/Err propagation.

Repository
View/report issues

Topics

#option #result #pattern-matching #rust-try-operator

Documentation

API reference

License

MIT (license)

More

Packages that depend on nidula