[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: questioning let
From: |
Tim X |
Subject: |
Re: questioning let |
Date: |
Thu, 25 Feb 2010 08:53:35 +1100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.1.92 (gnu/linux) |
Andreas Röhler <andreas.roehler@easy-emacs.de> writes:
> David Kastrup wrote:
>> Andreas Roehler <andreas.roehler@online.de> writes:
>>
>>> Hi,
>>>
>>> behaviour of the example code below puzzles me. Would
>>> expect setting of arg by external function, but inside
>>> `let', recognised. But remains `1'.
>>>
>>> (defun arg-setting ()
>>> (interactive)
>>> (let ((arg 1))
>>> (message "%s" arg)
>>> (arg-extern arg)
>>> (message "%s" arg)))
>>>
>>> (defun arg-extern (arg)
>>> (setq arg (1- arg)))
>>>
>>> Any help?
>>
>> The argument binding in arg-extern is the innermost one and consequently
>> the only affected one. If you make the function argument-less, it will
>> likely work as expected by you, affecting the binding in arg-setting.
>>
>
>
> That works, thanks a lot!
> However, stored in some eil.el, get a compiler warning than:
>
>
> In arg-extern:
> eil.el:9:9:Warning: reference to free variable `arg'
> eil.el:9:17:Warning: assignment to free variable `arg'
>
> Would think a useless warning, as the compiler should know being inside a let
> (?)
>
The compiler cannot know that this function will always be called in
such a way. All it knows is that at its time of definition, it is
setting an argument it doesn't know anything about.
>
The point to note is that in a defun argument list, that argument list
is essentially an implicit let. The 'arg' referenced inside your
original arg-extern is not the same as the arg being referenced in the
first function. this is why I suggest you change the argument name in
the arg-extern function as this will make that distinction obvious. To
get the value set in arg-extern returned to its caller, you would do
something like
(setq arg (arg-extern arg))
Tim
--
tcross (at) rapttech dot com dot au
Re: questioning let, Pascal J. Bourguignon, 2010/02/24
Re: questioning let, Tim X, 2010/02/24