tristate 1.0.0
tristate: ^1.0.0 copied to clipboard
An ultra-fast, zero-overhead Dart container for tri-state optional values, cleanly separating absent from explicitly null.
tristate #
An ultra-fast, zero-overhead Dart container for tri-state optional values—cleanly separating absent from explicitly null at sub-nanosecond speeds (~0.21 ns/op).
Used to represent optional values, cleanly separating the state of a value being unset (absent) from a value being explicitly null (e.g. for copyWith methods, patch requests, and optional parameters).
Features #
- Ultra-Fast & Zero-Overhead: Sub-nanosecond resolution (~0.21 ns/op), zero external dependencies, fully
const-constructible, and zero GC pressure. Val<T>(value): Wraps a given value (passnullto explicitly clear).Val<T>.absent(): Represents an absent / omitted value.- Callable instance
fooVal(current)to conveniently resolve fallback values when absent.
Usage #
Note:
Valis primarily intended for nullable fields (Val<T?>). Non-nullable fields typically do not needValbecause a standard nullable parameter (String? name) already usesnullto mean "keep current value" (name ?? this.name). PassingVal(null)to a non-nullableVal<T>will fail fast with aTypeError.
import 'package:tristate/tristate.dart';
class User {
final String name; // non-nullable: uses standard Dart
final String? bio; // nullable: uses Val to distinguish absent vs null
User({required this.name, this.bio});
User copyWith({
String? name,
Val<String?> bio = const Val.absent(),
}) {
return User(
name: name ?? this.name,
bio: bio(this.bio),
);
}
}
void main() {
final user = User(name: 'Ahmed', bio: 'Developer');
// Update non-nullable field directly
final u1 = user.copyWith(name: 'Bob');
print(u1.name); // Bob
print(u1.bio); // Developer
// Clear bio explicitly to null
final u2 = user.copyWith(bio: const Val(null));
print(u2.bio); // null
// Update bio with a new value
final u3 = user.copyWith(bio: const Val('Designer'));
print(u3.bio); // Designer
}
Performance Notes #
- Sub-Nanosecond Resolution: Resolving values via
val(current)compiles to devirtualized inlined leaf operations in AOT release mode, executing in ~0.21 ns/op without generic type-table lookup overhead. - Zero-Field Absent Instance:
Val.absent()contains zero instance fields, minimizing memory footprint and instance overhead. - Canonical
constAllocations:const Val.absent()andconst Val(...)reuse canonical instances at compile time, eliminating heap allocations and garbage collection pressure.
Pros & Cons #
Pros #
- Accurate Tri-State Handling: Cleanly separates omitted (absent) from explicitly null without ambiguity.
- Zero Dependencies: Pure Dart utility with zero external dependencies.
- No Code Generation: Works immediately in plain Dart without
build_runner,freezed, or reflection. - Full
constSupport: All constructors areconst-constructible for maximum efficiency. - Ergonomic Callable Syntax:
bio(this.bio)resolves values and fallback defaults in a single, concise expression.
Cons #
- Call-Site Wrapping: Dart lacks union types (
T | Val<T>), requiring callers to wrap arguments (Val(value)orconst Val(null)) for nullable fields. - Unnecessary for Non-Nullable Fields: Non-nullable fields (
String name) do not needVal, as standard DartString? namewithname ?? this.namealready handles the omitted state cleanly.