Skip to content

v0.4.0

Compare
Choose a tag to compare
@SandroMaglione SandroMaglione released this 16 Dec 04:50
· 197 commits to main since this release
201b3d0
  • Added extension methods to work with nullable types (T?)
    • From T? to fpdart's types
      • toOption
      • toEither
      • toTaskOption
      • toIOEither
      • toTaskEither
      • toTaskEitherAsync
      • fromNullable (Either, IOEither, TaskOption TaskEither)
      • fromNullableAsync (TaskEither)
    • From fpdart's types to T?
      • toNullable (Either)
/// [Option] <-> `int?`
int? value1 = 10.toOption().map((t) => t + 10).toNullable();

bool? value2 = value1?.isEven;

/// `bool?` -> [Either] -> `int?`
int? value3 = value2
    .toEither(() => "Error")
    .flatMap((a) => a ? right<String, int>(10) : left<String, int>("None"))
    .toNullable();

/// `int?` -> [Option]
Option<int> value4 = (value3?.abs().round()).toOption().flatMap(Option.of);
  • Added toIOEither to Either
  • Removed parameter from Either fromNullable [⚠️ BREAKING CHANGE]
final either = Either<String, int>.fromNullable(value, (r) => 'none');

/// 👆 Removed the value `(r)` (it was always null anyway 💁🏼‍♂️) 👇

final either = Either<String, int>.fromNullable(value, () => 'none');
  • Added chainEither to TaskEither
  • Added safeCast (Either and Option)
  • Added safeCastStrict (Either and Option)
int intValue = 10;

/// Unhandled exception: type 'int' is not a subtype of type 'List<int>' in type cast
final waitWhat = intValue as List<int>;
final first = waitWhat.first;

/// Safe 🎯
final wellYeah = Either<String, List<int>>.safeCast(
  intValue,
  (dynamic value) => 'Not a List!',
);
final firstEither = wellYeah.map((list) => list.first);