On this page:
guard
with-guard

12 Guard🔗

 (require koyo/guard) package: koyo-lib

This module provides utilities for implementing early returns from request handlers and other procedures.

syntax

(guard expr)

(guard expr #:else else-expr)
Within a with-guard form, ends execution early if expr is #f. If expr is not #f, it is returned normally. On early exit, when the first form is used, the result of the with-guard form is the application of form’s guard procedure. When the second form is used, the result of the with-guard form is else-expr. Use outside the body of a with-guard form is a syntax error.

Added in version 0.28 of package koyo-lib.

syntax

(with-guard guard-proc-expr
  body ...+)
Captures an escape continuation that is used to abort the execution of bodys when a guard condition fails. The guard-proc-expr is a procedure that is executed when a guard without an #:else keyword fails.

Examples:
> (require koyo/guard)
> (with-guard (lambda () 'not-found)
    (guard #f)
    (displayln "unreachable"))

'not-found

>
> (with-guard (lambda () 'not-found)
    (guard #t)
    (guard #f #:else 42)
    (displayln "unreachable"))

42

Added in version 0.28 of package koyo-lib.