help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: About Repeated Expansion


From: Drew Adams
Subject: RE: About Repeated Expansion
Date: Sun, 30 Dec 2012 09:07:29 -0800

> The GNU Emacs Lisp 
> manual(http://www.gnu.org/software/emacs/manual/html_node/elis
> p/Repeated-Expansion.html#Repeated-Expansion) says:
> 
>  Here is an example of how such side effects can get you into trouble:
>      (defmacro empty-object ()
>        (list 'quote (cons nil nil)))
>      (defun initialize (condition)
>        (let ((object (empty-object)))
>          (if condition
>              (setcar object condition))
>          object))
>
>  If initialize is interpreted, a new list (nil) is constructed 
>  each time initialize is called. Thus, no side effect survives 
>  between calls.  If initialize is compiled, then the macro 
>  empty-object is expanded during compilation, producing a 
>  single "constant" (nil) that is reused and altered each time 
>  initialize is called. 
> 
> It says if `initialize' is compiled, the (nil) will be reused 
> and altered each time `initialize' is called.  But there is 
> no expression that alters `empty-object', why will it be altered?

Yes, there is such an expression: (setcar object condition)

The empty object is a cons cell.  It is initialized with nil as its car and nil
as its cdr.  Each time `initialize' is called, it does (setcar object
condition), where OBJECT is precisely that same cons cell.  IOW, each time it is
called it sets the car of that cons cell to the value of CONDITION.

The point of this text is that macro `empty-object' returns a particular cons
cell, and if compiled that cons cell, not a call to `empty-object', is inserted
as a constant in the resulting byte-code.  So when `initialize' is then invoked,
its seeming call to `empty-object' just provides that same cons cell, which then
gets modified by `setcar'.

In interpreted code, the invocation of `empty-object' is still present, and it
returns a new cons cell (which then gets its car set).




reply via email to

[Prev in Thread] Current Thread [Next in Thread]