This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
Re: Macro-generated define ignored?
- From: Per Bothner <per at bothner dot com>
- To: Ito Kazumitsu <kaz at maczuka dot gcd dot org>
- Cc: kawa at sourceware dot org
- Date: Thu, 10 Oct 2013 13:41:04 -0700
- Subject: Re: Macro-generated define ignored?
- Authentication-results: sourceware.org; auth=none
- References: <20131011 dot 000339 dot 322954493 dot kaz at maczuka dot gcd dot org>
On 10/10/2013 08:03 AM, Ito Kazumitsu wrote:
Could you explain this case?
$ cat test.scm
(define-syntax foo
(syntax-rules ()
((_ obj) (define a (obj:toString)))))
(foo (java.lang.String "aaa"))
(display a)(newline)
$ java -cp kawa-1.14.1-svn.jar kawa.repl --script test.scm
aaa
$ java -cp kawa-1.14.1-svn.jar kawa.repl test.scm
test.scm:5:10: warning - no declaration seen for a
test.scm:5:10: unbound location a
This is an example of the power and the mystery of "macro hygiene".
In this case there is an 'a' that is local to the syntax-rules,
and there is an 'a' at the top-level scope. These are distinct scopes,
so the reference to 'a' at top-level does not "see" the definition
of 'a' generated by the macro.
One solution is to pass the name of the top-level variable as
a macro parameter:
(define-syntax foo
(syntax-rules ()
((_ var obj) (define var (obj:toString)))))
(foo a (java.lang.String "aaa"))
(display a)(newline)
You can replace 'var' by 'a' above if you prefer, but the
above is less confusing:
(define-syntax foo
(syntax-rules ()
((_ a obj) (define a (obj:toString)))))
(foo a (java.lang.String "aaa"))
(display a)(newline)
--
--Per Bothner
per@bothner.com http://per.bothner.com/