From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7698 invoked by alias); 10 Oct 2013 20:41:21 -0000 Mailing-List: contact kawa-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: kawa-owner@sourceware.org Received: (qmail 7686 invoked by uid 89); 10 Oct 2013 20:41:20 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.4 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: aibo.runbox.com Received: from aibo.runbox.com (HELO aibo.runbox.com) (91.220.196.211) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Thu, 10 Oct 2013 20:41:19 +0000 Received: from [10.9.9.207] (helo=mailfront03.runbox.com) by bars.runbox.com with esmtp (Exim 4.71) (envelope-from ) id 1VUN2o-0006Ze-QJ; Thu, 10 Oct 2013 22:41:14 +0200 Received: from 70-36-239-203.dsl.dynamic.sonic.net ([70.36.239.203] helo=localhost.localdomain) by mailfront03.runbox.com with esmtpsa (uid:757155 ) (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.76) id 1VUN2i-0008Lj-JC; Thu, 10 Oct 2013 22:41:08 +0200 Message-ID: <525710E0.9080202@bothner.com> Date: Thu, 10 Oct 2013 20:41:00 -0000 From: Per Bothner User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130805 Thunderbird/17.0.8 MIME-Version: 1.0 To: Ito Kazumitsu CC: kawa@sourceware.org Subject: Re: Macro-generated define ignored? References: <20131011.000339.322954493.kaz@maczuka.gcd.org> In-Reply-To: <20131011.000339.322954493.kaz@maczuka.gcd.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2013-q4/txt/msg00012.txt.bz2 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/