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

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

Re: How does letf work?


From: Joost Kremers
Subject: Re: How does letf work?
Date: 29 Jan 2014 09:14:38 GMT
User-agent: slrn/pre1.0.0-18 (Linux)

Joost Kremers wrote:
> And since the printing is done outside the letf, as you pointed out, the
> object that's printed is the one pointed to by the global binding of
> test-x. But that's not because outside the letf the object created
> inside it is necessarily gone. It's gone because letf doesn't return a
> pointer to it. If it does, the object can stay around longer, as
> demonstrated by returning the (cdr test-x).

[Apologies for the reply-to-self...]

This account also explains why the OP's code with setf works the way it does:

(let ((x (copy-list test-x)))
   (setf (cdr x) '(a b c d))
   x)

=> (KEY a b c d)

Because let creates a new binding (for x), a new pointer is created.
With copy-list, a new object is created to which this binding points.
After modifying the cdr of that object, the pointer is returned.
Outside the let, the binding for x is gone but the pointer to the object
it referred to is still alive, so the object itself is also kept alive
(until it's been printed).

--
Joost Kremers                                   joostkremers@fastmail.fm
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


reply via email to

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