JRR
JRR

Reputation: 6152

evaluate a condition when using syntax parse?

Is there an equivalent of #:when from match for syntax-parse? Wasn't able to find it in the documentation. Thanks.

Upvotes: 1

Views: 50

Answers (1)

Alexis King
Alexis King

Reputation: 43852

Yes, there is. It’s also called #:when. It even shows up when you search for #:when in the documentation search.

From the docs:

#:when condition-expr

Evaluates the condition-expr in the context of all previous attribute bindings. If the value is #f, the matching process backtracks. In other words, #:when is like #:fail-unless without the message argument.

Equivalent to #:post (~fail #:unless condition-expr #f).

Example:

> (syntax-parse #'(m 5)
    [(m x:number)
     #:when (even? (syntax-e #'x))
     #'x])
m: bad syntax
  in: (m 5)

Upvotes: 3

Related Questions