This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Macro-generated define ignored?


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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]