when<T, R> function

  1. @Nullable()
R when<T, R>(
  1. T value,
  2. List<WhenStatement<T, R>> statements
)

Implementation

@Nullable()
R when<T, R>(T value, List<WhenStatement<T, R>> statements) {
  Default<T, R> defaultStatement;
  for (var statement in statements) {
    if (statement is Default) {
      if (defaultStatement != null) {
        print("already has a default statement");
      }
      defaultStatement = statement;
    } else if (statement.satisfy(value)) {
      return statement.invoke();
    }
  }
  //if here, no return happened, call the de defaultStatement if found
  if (defaultStatement != null) {
    return defaultStatement.invoke();
  }

  return null;
}