assert(3scm) Scheme Programmer's Manual assert(3scm)

assert - raise an assertion violation if an expression is false

(import (rnrs))                     ;R6RS
(import (rnrs base))                ;R6RS

(assert expression)

An assert form is evaluated by evaluating expression. If expression returns #f, an exception with condition types &assertion and &message is raised. The message provided in the condition object is implementation-dependent.

Ikarus Scheme
See "BUGS".

If expression returns a true value, that value is returned from the assert expression.

(define (integer-add x y)
  (assert (and (integer? x) (integer? y)))
  (+ x y))
(integer-add 1 2)
   => 3
(integer-add 1 2.1)
   => raises &assertion and &message

Applications can use this form to guard against invalid uses of procedures. It is often more convenient than manual use of assertion-violation.

Implementations can exploit the fact that assert is syntax to provide as much information as possible about the location of the assertion failure.

This syntax can raise exceptions with the following condition types:
&assertion (R6RS)
The expression returned #f.
&syntax (R6RS)
More or fewer than one expression was passed.

assertion-violation(3scm)

R6RS

The assert macro is new in R6RS, but has a long history in programming.

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://weinholt.se/scheme/manpages/.

The implementation in psyntax from the r6rs-libraries project is buggy and does not return the true value. This bug has spread into some Scheme compilers.
2020-09-05