How to use assertions in CHICKEN code

At some point this file will hold some useful tutorial on how to include assertions in CHICKEN code, making it easy to detect errors in programs.

Please help us improve it!

Use assert

You can use assert with an expression that should always evaluate to true. For example,

 (define (string-first str)
   (assert (string? str))
   (string-ref str 0))

If the above function is called with, say, a number, you'll get the following error:

Error: assertion failed: (string? str)

Note that this doesn't always point to the function where the assertion failed. If you have multiple (string? str) assertions, you'll have to hunt it down. For this reason, I include the function's name in the assertion:

 (define (string-first str)
   (assert (and 'string-first (string? str)))
   (string-ref str 0))

In this case the error for the failed assertion will include the function's name, making it easier to find where it occurred. Of course, you will still need to find the caller...