assert(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

πŸ’… assert(3scm) β€” the Scheme Programmer's Manual, but make it cute

okay bestie, so this is assert(3scm) from the Scheme Programmer's Manual β€” and honestly? she's lowkey iconic. let's get into it 🫢


✨ NAME ✨

assert β€” raises an assertion violation when an expression comes back false. that's it, that's the gig πŸ’…


πŸ“š LIBRARY πŸ“š

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

both of these give you assert, so pick your vibe ✨


πŸŽ€ SYNOPSIS πŸŽ€

(assert expression)

one expression, no notes πŸ’–


πŸ¦‹ DESCRIPTION πŸ¦‹

so the way assert works is: it's a form, and evaluating it means evaluating expression. main character energy, right?

here's the tea though β€” if expression returns #f, an exception gets raised, and it carries the condition types &assertion and &message. slay for error handling.

ngl, the message that ends up inside that condition object is implementation-dependent β€” so don't come for the spec about it, she never promised you specific wording πŸ™ˆ


🧚 IMPLEMENTATION NOTES 🧚

  • Ikarus Scheme β€” see BUGS. yeah. that's the whole note. go look. πŸ’€

🌸 RETURN VALUES 🌸

if expression returns a true value, then that value is what the assert expression returns. periodt. you get your value back, no cap πŸ‘‘


⭐ EXAMPLES ⭐

(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

see how 1 2 ate and left no crumbs? and 1 2.1? caught in 4K with a &assertion and a &message 🚨 love that for us.


🌈 APPLICATION USAGE 🌈

apps can use this form to guard against invalid uses of procedures β€” like a little velvet rope at the door πŸŽ€ it's often more convenient than manually calling assertion-violation, fr fr.


πŸ’œ RATIONALE πŸ’œ

the big brain move here: because assert is syntax, implementations can exploit that fact to give you as much information as possible about where the assertion failure happened. positional info, on the house ✨


🚨 ERRORS 🚨

this procedure can raise exceptions with these condition types:

  • &assertion (R6RS) β€” the wrong number of arguments was passed, or an argument was outside its domain. classic.
  • &syntax (R6RS) β€” more or fewer than one expression was passed. one expression. one. i'm not asking twice 😭

πŸ‘€ SEE ALSO πŸ‘€


πŸ† STANDARDS πŸ†

R6RS πŸ’…


πŸ•°οΈ HISTORY πŸ•°οΈ

the assert macro is new in R6RS, but honestly she has a long history in programming. not a newbie, just newly specced ✨


🫢 AUTHORS 🫢

this page is part of the scheme-manpages project. it includes materials from the RnRS documents. more info can be found at https://github.com/schemedoc/manpages/. go show them some love πŸ₯Ή


πŸ’€ BUGS πŸ’€

the implementation in psyntax from the r6rs-libraries project is buggy and does not return the true value. and unfortunately this bug has spread into some Scheme compilers. not the contagion 😭 wash your hands, compilers.

and that's the page!! footer nation: this was 2020-09-05, version assert(3scm) β€” mwah, drink water, and remember to actually check your expressions before you assert them πŸ’–πŸŽ€πŸ¦‹πŸŒ·

πŸ“¦ where this page comes from