ContractOps<T> extension
Extension methods for Design by Contract preconditions and postconditions.
Design by Contract is a way to ensure functions and operations receive valid inputs and produce valid outputs. These methods provide fluent syntax for checking preconditions (conditions that must be true before using a value) and postconditions (conditions that must be true after an operation).
Important: Due to the try-finally implementation, both methods check their conditions in the finally block. While this ensures the check always runs, it means assertions occur AFTER the value is returned from the method. For traditional precondition checking (fail fast before use), consider checking conditions directly before the method call instead.
Example:
final user = getUser()
.pre(user.age >= 18, 'User must be adult')
.post((u) => u.email.contains('@'), 'Valid email required');
- on
-
- T
- @experimental
Methods
-
post(
bool condition(T), String message) → T -
Available on T, provided by the ContractOps extension
Checks a postcondition on this value and returns it. -
pre(
bool condition, String message) → T -
Available on T, provided by the ContractOps extension
Checks a precondition on this value and returns it.