when

Match Left or Right values using types

  1. This when(This either, Matcher matcher)
    This
    when
    (
    Matcher
    This : Either!(Left, Right)
    Left
    Right
    )
    (
    This either
    ,
    Matcher matcher
    )
    if (
    isCallable!Matcher
    )
  2. auto when(This either, T newValue)
  3. Either!(NewLeft!(This, T), NewRight!(This, T)) when(This either, T result)

Examples

'when' is called with the left value function when the monad isLeft is true

auto either = Either!(int, bool)(1);
string result = "none";

either
  .when((int value) {
    result = "left";
  })
  .when((bool value) {
    result = "right";
  });

result.should.equal("left");

it calls the 'when' with right value function when the monad isRight is true

auto either = Either!(int, bool)(true);
string result = "none";

either
  .when((int value) {
    result = "left";
  })
  .when((bool value) {
    result = "right";
  });

result.should.equal("right");

it does not call the 'when' function when the types don't match

auto either = Either!(int, bool)(true);
string result = "none";

either
  .when((double value) {
    result = "double";
  })
  .when((string value) {
    result = "string";
  });

result.should.equal("none");

it returns the binded value when the 'when' function returns

auto either = Either!(int, bool)(1);
bool message;

auto result = either
  .when((int value) {
    return true;
  })
  .when((bool value) {
    message = value;
  });

result.isRight.should.equal(true);
message.should.equal(true);

Meta