popeyelau / wiki

📒Wiki for many useful notes, source, commands and snippets.
3 stars 0 forks source link

Tuple & Either & Option #27

Open popeyelau opened 4 years ago

popeyelau commented 4 years ago

Tuple

const t = const Tuple2<String, int>('a', 10);

print(t.item1); // prints 'a'
print(t.item2); // prints '10'

Either & Option

 ///Either
 Either<SomeException, int> testEither(int code) {
    if (code >= 100) return Right(200);
    return Left(SomeException("不能小于 100"));
  }

 final result = testEither(100);
 print(result.isRight); //true
 print(result.isLeft); //false;
 result.fold((lhs) => print(lhs), (rhs) => print(rhs));