extension_type_unions
1.0.10, released 3 years ago by an unverified publisher
Untagged union types using Dart extension types.
WASM-ready
What it does
Provides generic extension types (Union2 through Union9) to represent untagged union types in Dart with zero runtime overhead. Offers split methods to pattern match and handle each operand safely.
AI-written summary of version 1.0.10, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need to restrict a function parameter to a specific set of types (e.g., int or String) without creating custom wrapper classes.
- You want compile-time type safety for union values with zero runtime allocation cost.
Skip it when
- You require traditional algebraic property support like subtyping and order-independence out of the box.
- You want implicit constructor syntax which is not yet supported natively by Dart.
Strengths
- Zero runtime overhead because it leverages Dart extension types.
- Provides static type checks for handling union cases via split methods.
- Includes a bounded variant for improved run-time type safety.
Weaknesses
- Lacks traditional algebraic properties and implicit constructors.
- Extension types can have their encapsulation violated via manual type casts.
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- No
- Direct dependencies
- 0
import 'package:extension_type_unions/extension_type_unions.dart';
int f(Union2<int, String> x) => x.split(
(i) => i + 1,
(s) => s.length,
);
void main() {
print(f(1.u21));
print(f('Hello'.u22));
}Alternatives
Packages that do the same job and could replace extension_type_unions.
- validators2
String validation and sanitization for Dart.
83Compare - tint
Terminal string styling as an extension on String.
50Compare - result_type
A Result type representing success or failure in Dart
50Compare - time
Type-safe DateTime and Duration calculations, powered by extensions.
70Compare