runWithChecking<R> function
R
runWithChecking<R>({
- required bool preCondition(),
- required R operation(),
- required bool postCondition(
- R result
- ContractCheckingMode checkingMode = .assertionsOnly,
Runs operation after checking preCondition and checks its result.
In ContractCheckingMode.assertionsOnly, failed conditions trigger
Dart assertions and therefore depend on assertions being enabled. In
ContractCheckingMode.alwaysThrow, failed conditions throw
ContractViolationError. Defaults to ContractCheckingMode.assertionsOnly.
Set checkingMode to select the checking behavior. When assertions are disabled,
the condition callbacks are not invoked in
ContractCheckingMode.assertionsOnly.
Exceptions thrown by a condition or by operation propagate unchanged.
Implementation
R runWithChecking<R>({
required bool Function() preCondition,
required R Function() operation,
required bool Function(R result) postCondition,
ContractCheckingMode checkingMode = .assertionsOnly,
}) {
switch (checkingMode) {
case .assertionsOnly:
assert(preCondition(), 'Precondition violated');
final result = operation();
assert(postCondition(result), 'Postcondition violated');
return result;
case .alwaysThrow:
if (!preCondition()) {
throw ContractViolationError(
kind: .preCondition,
message: 'Precondition violated',
);
}
final result = operation();
if (!postCondition(result)) {
throw ContractViolationError(
kind: .postCondition,
message: 'Postcondition violated',
);
}
return result;
}
}