std/core/checks
std/checks — runtime assertions / preconditions / postconditions.
Each helper throws on violation rather than aborting, so user
code can catch and recover when appropriate. The caller'smessage is forwarded into the exception, so e.message reads
it back in the catch handler.
Naming follows Objects.requireNonNull /assert / Preconditions.checkArgument conventions:
• require* = precondition (throws IllegalArgumentException)
• check* = state invariant (throws IllegalStateException)
• ensure* = postcondition (throws IllegalStateException)
Free functions
| Type | Method and description |
|---|---|
| requireTrue(condition : bool, message : string) : void Require condition to hold; throw IllegalArgumentException(message)otherwise. |
| requireFalse(condition : bool, message : string) : void Require condition to be false; throw IllegalArgumentException(message)otherwise. |
| requirePositive(value : i64, message : string) : void Require value > 0; throw IllegalArgumentException(message) otherwise.Zero is rejected (use requireNonNegative to allow it). |
| requireNonNegative(value : i64, message : string) : void Require value >= 0; throw IllegalArgumentException(message) otherwise. |
| requireInRange(
value : i64,
min : i64,
max : i64,
message : string
) : void Require min <= value <= max (both bounds inclusive); throwIllegalArgumentException(message) otherwise. |
| checkState(condition : bool, message : string) : void State invariants (different exception type so catch handlers can distinguish a bad input from a bad object state). |
| fail(message : string) : void Hard fail — never returns. Useful for "unreachable" branches the type checker can't prove dead. |
| requireNonEmpty(s : string, message : string) : void Require s to be non-empty; throw IllegalArgumentException(prefixed with the check name) otherwise. |
Method detail
#requireTrue
Require condition to hold; throw IllegalArgumentException(message)
otherwise.
condition precondition that must be true to passmessage text forwarded into the exception when it fails#requireFalse
Require condition to be false; throw IllegalArgumentException(message)
otherwise.
condition precondition that must be false to passmessage text forwarded into the exception when it fails#requirePositive
Require value > 0; throw IllegalArgumentException(message) otherwise.
Zero is rejected (use requireNonNegative to allow it).
value number that must be strictly greater than 0message text forwarded into the exception when it fails#requireNonNegative
Require value >= 0; throw IllegalArgumentException(message) otherwise.
value number that must be zero or greatermessage text forwarded into the exception when it fails#requireInRange
Require min <= value <= max (both bounds inclusive); throwIllegalArgumentException(message) otherwise.
value number checked against the inclusive boundsmin inclusive lower boundmax inclusive upper boundmessage text forwarded into the exception when it fails#checkState
State invariants (different exception type so catch handlers can
distinguish a bad input from a bad object state).
condition state invariant that must be true to passmessage text forwarded into the IllegalStateException#fail
Hard fail — never returns. Useful for "unreachable" branches the
type checker can't prove dead.
message text forwarded into the thrown IllegalStateException#requireNonEmpty
Require s to be non-empty; throw IllegalArgumentException
(prefixed with the check name) otherwise.
s string that must have a non-zero byte lengthmessage detail appended after the "requireNonEmpty: " prefix