either 0.1.8

  • README.md
  • Installing
  • Versions
  • 42

Either

Build Status

A more expressive way to return error or success values via the type system. Instead of throwing exeptions return an error state of any type, even if it conflicts with the type of the success return value. The higher kinded type of the Either will be used to determine if the value contains an error or a success value.

Either is an interface implemented by the two concrete types of Left and Right. By convention, Left is used to encapsulate an error value while Right is used to encapsulate a success value.

Left and Right are both projectable to Option types so you can chain and compose operations together based on success or failure values in the typical monadic flow style via Option#map, Option#flatMap, etc.

Example

import 'package:either/either.dart';

main() {

  var left  = new Left<String, String>("left");
  var right = new Right<String, String>("right");

  var leftIsLeft          = left.isLeft();
  var leftIsRight         = left.isRight();
  var leftLeftProjection  = left.left();
  var leftRightProjection = left.right();
  var leftSwapped         = left.swap();
  var leftFold    = left.fold(
    (v) => "folded ${v}",
    (v) => "never executed"
  );

  var rightIsLeft          = right.isLeft();
  var rightIsRight         = right.isRight();
  var rightLeftProjection  = right.left();
  var rightRightProjection = right.right();
  var rightSwapped         = right.swap();
  var rightFold            = right.fold(
    (v) => "never executed",
    (v) => "folded ${v}"
  );

}

Public Interface

This is the Either interface implemented by both Left and Right

part of option;

import 'package:option/option.dart';

abstract class Either<L, R> {

  /**
   * Returns true if this `Either` type is `Left`, fasle otherwise
   *
   * @return {Boolean}
   */
  bool isLeft();

  /**
   * Returns true if this `Either` type is `Right`, false otherwise
   *
   * @return {Boolean}
   */
  bool isRight();

  /**
   * If this `Either` type is `Left` then `leftCase` will be called with the
   * inner value of `Left` and the result of `leftCase` will be returned. The
   * same applies for `rightCase` in the event that this `Either` type
   * is `Right`
   *
   * @param {dynamic(L)} leftCase  - The computation to run on `Left` type
   * @param {dynamic(R)} rightCase - The computation to run on `Right` type
   * @return {dynamic}             - The result of the computation that was ran
   */
  dynamic fold(dynamic leftCase(L left), dynamic rightCase(R right));

  /**
   * Returns an `Option` projection of the `Left` value of this `Either` type.
   * So if this is type `Left` it returns an instance of `Some` but if this is
   * a `Right` type this returns an instance of `None`
   *
   * @return {Option<L>} - The optional left projection
   */
  Option<L> left();

  /**
   * Returns an `Option` projection of the `Right` value of this `Either` type.
   * So fi this is type `Right` it returns an instance of `Some` but if this is
   * a `Left` type thin this returns an instance of `None`
   *
   * @return {Option<R>} - The optional right projection
   */
  Option<R> right();

  /**
   * When ran on a `Left` type this returns a `Right` with the same inner value.
   * When ran on a `Right` type this returns a `Left` with the same inner value.
   *
   * @return {Either<R, L>} - The swapped `Either` type
   */
  Either<R, L> swap();

}

1. Depend on it

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


dependencies:
  either: "^0.1.8"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:either/either.dart';
        
Version Uploaded Documentation Archive
0.1.8 Aug 16, 2014 Go to the documentation of either 0.1.8 Download either 0.1.8 archive
0.1.7 May 4, 2013 Go to the documentation of either 0.1.7 Download either 0.1.7 archive
0.1.6 May 4, 2013 Go to the documentation of either 0.1.6 Download either 0.1.6 archive
0.1.5 Apr 13, 2013 Go to the documentation of either 0.1.5 Download either 0.1.5 archive
0.1.4 Mar 18, 2013 Go to the documentation of either 0.1.4 Download either 0.1.4 archive
0.1.3 Mar 17, 2013 Go to the documentation of either 0.1.3 Download either 0.1.3 archive
0.1.2 Mar 17, 2013 Go to the documentation of either 0.1.2 Download either 0.1.2 archive
0.1.1 Mar 17, 2013 Go to the documentation of either 0.1.1 Download either 0.1.1 archive
0.1.0 Mar 16, 2013 Go to the documentation of either 0.1.0 Download either 0.1.0 archive

Analysis

This feature is new.
We welcome feedback.

We analyzed this package, and provided a score, details, and suggestions below.

  • completed on Dec 6, 2017
  • Dart: 2.0.0-dev.8.0
  • pana: 0.7.3+1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
28
Health:
Code health derived from static analysis. [more]
93
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall score:
Weighted score of the above. [more]
42

Platforms

Detected platforms: Flutter, server, web

All libraries agree

Suggestions

  • Maintain CHANGELOG.md.

    Changelog entries help clients to follow the progress in your code.

Dependencies

Package Constraint Resolved Available
Direct dependencies
option >=1.0.0 <2.0.0 1.2.0
Transitive dependencies
concepts 0.2.0
Dev dependencies
unittest 0.4.7+1