option 0.3.2

Option #

The option typeclass inspired by scala's implementation and Haskell's Maybe monad. Option is a composable and more expressive alternative to things that may be null on absence of value.

Tested with Dart 0.4.7

Examples #

import 'package:option/option.dart';

main() {
  final some   = new Some(42);
  final inner  = some.get();
  final mapped = some.map((n) => n / 2);
  final toSome = some.flatMap((n) => new Some(12));
  final toNone = some.flatMap((n) => new None<num>());
  final passed = some.filter((n) => (n % 2) == 0);
  final failed = some.filter((n) => (n % 2) != 0);
  final none   = new None();
  final alt1   = none.getOrElse(42);
  final alt2   = none.orElse(() => new Some(33));
  final alt3   = none.orElse(new Some(32));

  print('secret to all ${some.getOrElse(12)}');

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  option: ^0.3.2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:option/option.dart';
Awaiting analysis to complete.