You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(define/with-datum (x ((y z ...) ...))
'("X" (("Y1""Z11""Z12")
("Y2""Z21"))))
This code uses define/with-datum, the counterpart of define/with-syntax, to define pattern variables bound to plain data. Just like define/with-syntax, define/with-datum allows arbitrary patterns (with ellipses) that syntax-case supports.
In this example, the pattern variable x matches "X" in the list while the pattern ((y z ...) ...) defines the pattern variables y, z and matches (("Y1" "Z11" "Z12") ("Y2" "Z21")). The variable y at ellipsis depth 1 is bound to the value ("Y1" "Y2") and the variable z at ellipsis depth 2 is bound to the value (("Z11" "Z12") ("Z21")).
Here are a few examples using the defined pattern variables in datum templates:
(datum x)
(with-datum ([w "W"]) ;; currently supported
(datum ((y w) ...)))
(datum
((^ z ... $) ...))
All the procedural code in the original macro can be expressed in the pattern directives, #:attr, #:do and #:with. These pattern directives allow escaping into arbitrary expansion-time computation while retaining appropriate semantical meanings such as binding a pattern variable (#:with) or performing an imperative action (#:do).
Macro
The original macro: https://github.com/racket/racket/blob/8e83dc25f7f5767d9e975f20982fdbb82f62415a/racket/collects/racket/syntax.rkt#L22-#L59
A new implementation using
syntax-parse
:Example
This code uses
define/with-datum
, the counterpart ofdefine/with-syntax
, to define pattern variables bound to plain data. Just likedefine/with-syntax
,define/with-datum
allows arbitrary patterns (with ellipses) thatsyntax-case
supports.In this example, the pattern variable
x
matches"X"
in the list while the pattern((y z ...) ...)
defines the pattern variablesy
,z
and matches(("Y1" "Z11" "Z12") ("Y2" "Z21"))
. The variabley
at ellipsis depth 1 is bound to the value("Y1" "Y2")
and the variablez
at ellipsis depth 2 is bound to the value(("Z11" "Z12") ("Z21"))
.Here are a few examples using the defined pattern variables in datum templates:
Before and After
The original macro: https://github.com/racket/racket/blob/8e83dc25f7f5767d9e975f20982fdbb82f62415a/racket/collects/racket/syntax.rkt#L22-#L59
All the procedural code in the original macro can be expressed in the pattern directives,
#:attr
,#:do
and#:with
. These pattern directives allow escaping into arbitrary expansion-time computation while retaining appropriate semantical meanings such as binding a pattern variable (#:with
) or performing an imperative action (#:do
).Licence
I hereby license the code in this issue under the same MIT License that the Racket language uses:
https://github.com/racket/racket/blob/master/racket/src/LICENSE-MIT.txt
All the associated text in this issue is licensed under the Creative Commons Attribution 4.0 International License http://creativecommons.org/licenses/by/4.0/
The text was updated successfully, but these errors were encountered: