Skip to content
Turtle Kitty edited this page Aug 9, 2015 · 2 revisions

guard

This operator places a new handler on the error continuation.
If an error is thrown, this handler will be called with an error argument and a retry continuation.
If the handler returns a value, this becomes the value of the (guard ...) form.
If the handler calls the retry continuation with a new value, the computation that threw the error is retried.
If the handler throws an error itself, it goes to the next handler up on the error continuation.

(fun fun-handler (err kontinue)
    (if (= err 'resume)
        (kontinue 42)
        (if (= err 'default)
            69
            (error 'uh-oh))))

; no error

(+ 1
    (guard
        fun-handler
        (+ 2 3))) -> 6

; default
    
(+ 1
    (guard 
        fun-handler
        (+ 2 (error 'default)))) -> 70

; continue

(+ 1
    (guard
        fun-handler
        (+ 2 (error 'resume)))) -> 45

; abort

(+ 1
    (guard
        fun-handler
        (+ 2 (error 'crap)))) -> ERROR 'uh-oh

Sexy programmers should beware infinite loops when retrying errors.

Clone this wiki locally