sum_types_generator 0.1.5+1 copy "sum_types_generator: ^0.1.5+1" to clipboard
sum_types_generator: ^0.1.5+1 copied to clipboard

outdated

sum_types and sum_types_generator packages together define a code generator enabling sum-types in Dart.

Build Status sum_types version sum_types_generator version

sum_types and sum_types_generator #

sum_types and sum_types_generator packages together define a code generator enabling sum-types in Dart.

Example #

In example/lib/src/ you can find a few sum-type declarations and the code generated for them.

This one models the natural numbers (with zero):

@SumType([
  Case<void>(name: "zero"),
  Case<_Nat>(name: "next"),
])
mixin _Nat implements _NatBase {
  Nat operator +(Nat other) => iswitch(
        zero: () => other,
        next: (next) => Nat.next(next + other),
      );

  int toInt() => iswitch(
        zero: () => 0,
        next: (next) => 1 + next.toInt(),
      );
}

Features #

Core:

  • Const case-constructors (const Nat.zero())
  • Extensible sum-types (Nat.toInt())
  • Nested sum-types
  • Recursive sum-types (Case<_Nat>(name: "next")Nat.next(Nat.zero()))
  • Generic sum-types (Either<Left, Right>)
  • Exhaustive in-line iswitch
  • Inexhaustive in-line iswitcho (with otherwise: case)

Sugar:

  • No-payload cases (Case<void>(name: "zero")Nat.zero())
  • Default case-names (Case<String>()JSON.string("some"))

Trivia:

  • Equality test
  • Hash function
  • To string conversion

Serialization-deserialization support through product-types interoperability:

  • Deserialization support (NatRecord<Self>, Nat.load<T extends NatRecord<T>>(T rec))
  • Serialization support (Nat.dump<T>(T Function({Unit zero, T next} make)))

Development #

Find the upcoming development plans in the project planner.

4
likes
0
pub points
57%
popularity

Publisher

unverified uploader

sum_types and sum_types_generator packages together define a code generator enabling sum-types in Dart.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

analyzer, build, meta, source_gen, sum_types

More

Packages that depend on sum_types_generator