sum_types
0.4.0, released 2 years ago by an unverified publisher
A code generator that adds algebraic sum‑type support to Dart.
What it does
The package provides annotations and a generator to create immutable sum‑type classes with exhaustive pattern matching. It also adds utilities like equality, hashing, and (de)serialization for the generated types.
AI-written summary of version 0.4.0, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need algebraic data types (union types) with exhaustive switch handling in Dart/Flutter.
- You want compile‑time safety and const constructors for variant classes.
Skip it when
- You prefer a runtime‑only solution without build steps.
- Your project cannot add build_runner or code‑gen dependencies.
Strengths
- Generates boiler‑plate‑free sum‑type classes.
- Supports const constructors, generic and recursive types.
- Provides exhaustive `iswitch` pattern matching.
Weaknesses
- Requires build_runner and the generator to run.
- Only works with Dart SDK >=3.0.0.
- Limited to sum‑type use‑cases; not a general utility library.
Setup
- Kind
- Pure Dart
- Effort
- Medium
- Code generation
- No
- Direct dependencies
- 1
Add `sum_types` and `sum_types_generator` to pubspec, run `flutter pub add build_runner`, then execute `flutter pub run build_runner build`.
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);
}
Alternatives
Packages that do the same job and could replace sum_types.
- pigeon
Type-safe code generator for communication between Flutter and host platforms.
90Compare - tonik
Pure Dart OpenAPI 3.0/3.1 code generator for type-safe API client packages.
74Compare - flutter_gen_runner
Generates strongly-typed Dart classes for Flutter assets, fonts, and colors to avoid string-based APIs.
87Compare - build
Package for authoring build_runner compatible code generators.
87Compare
Works with
Packages commonly used together with sum_types: ones it builds on, ones built on it, and its own family.