when

Match Left or Right values using a check function

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

Examples

it calls the 'when' function when the function check returns true for Right value

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

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

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

it calls the 'when' with the value function when the function check returns true for Right value

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

auto result = either
  .when!alwaysTrue ((bool value) {
    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 function check returns false for Right value

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

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

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

it calls the 'when' function when the function check returns true for Left value

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

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

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

Meta