when

Match Left or Right values by value examples

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

Examples

it is called when the Either value matches the when!Left value

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

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

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

it is not called when the value matches the Left

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

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

result.isLeft.should.equal(true);
message.should.equal(false);

it is called when the value matches the Right

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

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

result.isLeft.should.equal(true);
message.should.equal(2);

it does not call the 'when' function when the value matches the Left

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

auto result = either
  .when!false ({
    return 3;
  })
  .when((bool value) {
    message = value;
  });

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

it returns the 'when' value when the value matches the Right

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

auto result = either
  .when!true (2)
  .when((int value) {
    message = value;
  });

result.isLeft.should.equal(true);
message.should.equal(2);

it does not return the 'when' value when the value is not matched

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

auto result = either
  .when!3 (true)
  .when((bool value) {
    message = value;
  });

result.isLeft.should.equal(true);
message.should.equal(false);

it does not return the 'when' value when the value is not matched

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

auto result = either
  .when!false (3)
  .when((bool value) {
    message = value;
  });

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

it returns the 'when' value when the value matches the Left

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

auto result = either
  .when!1 (true)
  .when((bool value) {
    message = value;
  });

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

it returns the new Either type

auto either = 5.bind;

expect(typeof(either).stringof).to.equal("Either!(Any, int)");

auto result = either.when!(5) ("The value is 5!".bind!(string, int));

expect(typeof(result).stringof).to.equal("Either!(string, int)");
expect(result.left).to.equal("The value is 5!");

returning a left value with any

auto result = 0.bind.when!(0) ("got zero!".bindLeft);

result.isLeft.should.equal(true);
result.left.should.equal("got zero!");

Meta