On this page:
guard
with-guard
define-guard

13 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. Use outside of the body of a with-guard form is a syntax error.

On early exit, when the first form is used, the result of the with-guard form is the application of the form’s guard procedure. When the second form is used, the result of the with-guard form is else-expr.

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.

syntax

(define-guard (guard-id arg-id ...)
  maybe-else
  guard-expr)
 
maybe-else = 
  | #:else else-expr
Defines a reusable guard.

Examples:
> (require koyo/guard)
> (define-guard (guard-positive x)
    #:else 'fail-not-positive
    (> x 0))
> (with-guard (lambda () 'fail)
    (guard-positive 5)
    (println 'after-5)
    (guard-positive 0))

'after-5

'fail-not-positive

Added in version 0.49 of package koyo-lib.