A new syntax object can be constructed by both syntax/loc and syntax (also written #').
When should I use syntax/loc?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
#'(i.e.syntax) when you are not constructing a new syntax object, as when you’re just referencing a pattern variable bound withsyntax-caseorwith-syntax.Use
#'for temporary syntax objects as in(syntax->list #'(id ...)).Use
#'for syntax objects representing forms that you know won’t have syntax errors in them, or where syntax errors in them are the fault of your macro implementation, not the use of your macro.Use
syntax/locwhen you construct expressions that potentially can contain syntax-errors due to an incorrect usage of your macro.Let’s consider a concrete example:
The form
display-letshould work exactly like normallet, except that it displays the values of the bindings before it evaluates the body.Here is a first implementation:
Here is an example of a correct use of the macro:
Now let us see what happens when the macro is used incorrectly:
This usage is incorrect, since a use of
letmust always have a non-empty body. Besides printing the error message, DrRacket colors this code red:Although it is correct that the
lambdaexpression constructed by the macro is incorrect,(lambda ())is not legal, it is not due to an error in the macro, but due to an incorrect use of the macro.To redirect the blame surround the constructed
lambdaexpression withsyntax/locand use the first argument ofsyntax/locas place to color red.This time around the
(display-let-2 ())entered in the repl is colored red, and the error message mentioneddisplay-let-2instead oflambda.