scm: add if-let/when-let macros

Add if-let/if-let*/when-let/when-let* macros for internal use in
mu-scm.el.
This commit is contained in:
Dirk-Jan C. Binnema
2026-07-29 23:39:11 +03:00
committed by Seth Ladygo
parent 41f999a6bf
commit 8c9734066f

View File

@ -126,6 +126,40 @@
;; ;;
;; Helpers ;; Helpers
(define-syntax if-let*
(syntax-rules ()
"Bind variables according to VAR and evaluate THEN or ELSE.
Evaluate each binding in turn, as in let*, stopping if a binding value is nil.
If all are non-nil return the value of THEN, otherwise the value of the last
form in ELSE, or #f if there are none."
((_ () then) then)
((_ () then else) then)
((_ ((var expr) rest ...) then)
(if-let* ((var expr) rest ...) then #f))
((_ ((var expr) rest ...) then else)
(let ((var expr))
(if var
(if-let* (rest ...) then else)
else)))))
(define-syntax when-let*
(syntax-rules ()
"Bind variables according to VAR and evaluate THEN.
Evaluate each binding in turn, as in let*, stopping if a binding value is nil.
If all are non-nil return the value of THEN, or #f if there are none."
((_ (bindings ...) body body* ...)
(if-let* (bindings ...) (begin body body* ...)))))
(define-syntax if-let
(syntax-rules ()
"Alias for if-let*."
((_ args ...) (if-let* args ...))))
(define-syntax when-let
(syntax-rules ()
"Alias for when-let*."
((_ args ...) (when-let* args ...))))
(define (set-documentation! symbol docstring) (define (set-documentation! symbol docstring)
"Set the docstring for symbol in current module to docstring. "Set the docstring for symbol in current module to docstring.
This is useful for symbols that do not support docstrings directly, such This is useful for symbols that do not support docstrings directly, such