define-expression-macro defines a macro that expands into a Coalton expression.
Syntax
(define-expression-macro ⟨name⟩ ⟨lambda-list⟩
⟨docstring⟩?
⟨declaration⟩*
⟨lisp-body⟩+)
Semantics
define-expression-macrois a Common Lisp top-level form (used outsidecoalton-toplevel).⟨lambda-list⟩is a standard Common Lisp macro destructuring lambda list. (When writing macros in a package that uses#:coaltonrather than#:cl, qualify lambda-list keywords such ascl:&optional,cl:&rest,cl:&body, andcl:&key.)- The macro body executes as Common Lisp at compile time and must return a single s-expression representing a valid Coalton expression.
- When
⟨name⟩appears as the operator of a list in a Coalton expression context, the parser expands the macro and recursively parses the returned form as a Coalton expression. - Nested expression macro expansions are capped at 500 expansion steps per expression to catch infinite expansion loops.
- Hygiene is manual: use
cl:gensymoralexandria:with-gensymsfor temporary Coalton bindings in the expansion to avoid variable capture and duplicate evaluation.
Examples
;; Bind `name` when an Optional is `Some`, or evaluate `else`
(define-expression-macro if-some ((name expr) then else)
"Evaluate `then` with `name` bound if `expr` is `Some`, otherwise `else`."
`(match ,expr
((Some ,name) ,then)
((None) ,else)))
(coalton
(if-some (x (Some 41))
(+ x 1)
0))
; => 42
;; Avoid evaluating an argument more than once with gensym
(define-expression-macro square-expr (x)
(cl:let ((tmp (cl:gensym "X-")))
`(let ((,tmp ,x))
(* ,tmp ,tmp))))
Other notes
define-expression-macrorecords:expressionas the symbol’s:coalton-repl-contextproperty so Coalton-aware editors, REPLs, and documentation generators recognize it as an expression macro.- For macros that expand into top-level definitions (
define,declare,define-instance, etc.), usedefine-toplevel-macro. - See Macros for more details on writing macros in Coalton.