assert: Generate an error if an expression is not true.
Description
Generate an error if an expression is not true.
Usage
assert(FLAG)
Arguments
FLAG
Expression that should evaluate to a boolean vector
Value
None. Evaluated only for its side effect.
Details
Assert generate an error if its aregument does not evaluate to
boolean (vector) containing only TRUE values. This is useful
for defensinve programming as it provides a mechanism for checking
that certain facts, the 'assertions', do in fact hold. Checking of
'assertions' is an important tool in the development of robust program
code.
## Trivial exampleposSqrt <- function(x)
{
assert(x>=0)
sqrt(x)
}
posSqrt(1:10) # works fine, no messagesposSqrt(-5:5) # generates an error, since the asssertion is not met