sum_types_generator 0.1.2+2 copy "sum_types_generator: ^0.1.2+2" to clipboard
sum_types_generator: ^0.1.2+2 copied to clipboard

outdated

A code generator enabling sum-types in Dart.

Build Status Pub

sum_types #

A code generator enabling sum-types in Dart.

Features #

Core:

  • Const-constructors
  • Extensible sum-types (Nat.toInt())
  • No-payload cases (Case<void>(name: "zero")Nat.zero())
  • Default case-names (Case<String>()JSON.string("some"))
  • 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)

Trivia:

  • Equality test
  • Hash function
  • To string conversion

Example #

In example/lib/example.dart you can find a couple of sum-type declarations and in example/lib/example.g.dart the generated code.

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(),
      );
}

Development #

Find the upcoming development plans in the project planner.

4
likes
0
pub points
59%
popularity

Publisher

unverified uploader

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