Invariant mixin
Design by Contract: Mixin for enforcing object invariants.
An invariant is a condition that must always be true for an object throughout its lifetime. By implementing Invariant, you declare what invariants your class maintains.
Use this mixin to:
- Document and enforce class invariants
- Catch state corruption early during development
- Ensure consistency after state-modifying operations
Example:
class BankAccount with Invariant {
double _balance;
BankAccount(this._balance);
void withdraw(double amount) {
_balance -= amount;
checkInvariant('withdraw');
}
@override
bool invariant() => _balance >= 0; // Balance must never be negative
}
- @experimental
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
checkInvariant(
String operation) → void -
Asserts that the invariant is satisfied for
operation. -
invariant(
) → bool - The invariant condition that must always be true for this object.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited