werediver/sum_types.dart
A code generator enabling sum-types in Dart
- Stars
- 40
- Forks
- 7
- Last push (UTC)
- Nov 12, 2024
- Project status
- Active
LanguagesDartShell
Technical topics
Dependencies used
Dependency list 1 items
- housekeeperDevelopment
{"path":"housekeeper"}
Original README
English project snapshot. Visit GitHub for the latest content.
Expand / collapse project README
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.
NOTE: v0.2.0 is a major update with backward-incompatible changes.
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):
import 'package:meta/meta.dart';
import 'package:sum_types/sum_types.dart';
@SumType()
class Nat extends _$Nat {
const Nat.zero() : super(zero: const Unit());
const Nat.next(Nat value) : super(next: value);
Nat operator +(Nat other) => this.iswitch(
zero: () => other,
next: (next) => Nat.next(next + other),
);
int toInt() => this.iswitch(
zero: () => 0,
next: (next) => 1 + next.toInt(),
);
}
Features
Core:
- [x] Const case-constructors (
const Nat.zero()) - [x] Extensible sum-types (
Nat.toInt()) - [x] Nested sum-types
- [x] Recursive sum-types (
Case<_Nat>(name: "next")→Nat.next(Nat.zero())) - [x] Generic sum-types (
Either<Left, Right>) - [x] Exhaustive in-line
iswitch - [x] Inexhaustive in-line
iswitcho(withotherwise:case)
Sugar:
- [x] No-payload cases (
Case<void>(name: "zero")→Nat.zero()) - [x] Default case-names (
Case<String>()→JSON.string("some"))
Trivia:
- [x] Equality test
- [x] Hash function
- [x] To string conversion
Serialization-deserialization support through product-types interoperability:
- [x] Deserialization support (
NatRecord<Self>,Nat.load<T extends NatRecord<T>>(T rec)) - [x] Serialization support (
Nat.dump<T>(T Function({Unit zero, T next} make)))
Development
Find the upcoming development plans in the project planner.