contract library

Runtime checks for preconditions, postconditions, and object invariants.

Use runWithChecking for a single operation, or Invariant when checks belong to an object's lifecycle. Choose ContractCheckingMode.alwaysThrow when checks must remain active with assertions disabled.

Classes

Invariant
Provides checks for object invariants, preconditions, and postconditions.

Enums

ContractCheckingMode
Selects how contract violations are handled.
ViolationKind
Identifies which part of a contract was violated.

Functions

runWithChecking<R>({required bool preCondition(), required R operation(), required bool postCondition(R result), ContractCheckingMode checkingMode = .assertionsOnly}) → R
Runs operation after checking preCondition and checks its result.
runWithCheckingAsync<R>({required bool preCondition(), required Future<R> operation(), required bool postCondition(R result), ContractCheckingMode checkingMode = .assertionsOnly}) → Future<R>
Runs operation after checking preCondition and checks its result.

Exceptions / Errors

ContractViolationError
An error raised when a contract check fails in ContractCheckingMode.alwaysThrow.