[plt-scheme] Non-hygenic Macro Revelation

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Thu Mar 25 16:29:52 EDT 2010

At Thu, 25 Mar 2010 12:41:58 -0600, Jay McCarthy wrote:
> I've always used the pattern
> (datum->syntax stx (string->symbol (format "~a?" (syntax->datum #'id))))
> when defining, for example, predicates of inputs to macros. (stx is
> the syntax object given to the transformer.)
> [...]
> In this example x? is unbound, because it gets the lexical context of
> the define-pred-wrap macro, not the user's program. This can be easily
> fixed by using
> (datum->syntax #'id (string->symbol (format "~a?" (syntax->datum #'id))))
> [...]
> Is there a convention I don't know about?

I don't think I've ever seen a case where the former is preferable. The
latter has always seems like the right choice in the macros that I've
written. (So, I agree with Carl's ordering of the options.)

> One
> idea is to use stx when the new identifier is based on multiple
> inputs, but otherwise use the context of the base identifier.

Multiple contexts show up with `define-struct', for example, where a
selector name is formed from the type name plus a field name. In that
case it's less clear, but using one of the identifiers still seems
best. In the case of `define-struct' selectors and mutators, the type
name's context is used. (That's not documented, currently, but I'll fix
the docs.)

Posted on the users mailing list.