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 08:37:45 GMT
User-agent: slrn/pre1.0.0-18 (Linux)

Michael Heerdegen wrote:
> Florian Beck <fb@miszellen.de> writes:
>
>> (letf (((cdr test-x) '(a b c d)))
>>   test-x)
>>
>> = > (KEY 1 2 3 4)
>
> Though paradoxical, it seems right to me.  You return a list that you
> modified temporarily, but when the result is printed in the echo area,
> the letf has been left and the structure of the list has changed back.

In essence, yes, but it's not as simple as all that:

(letf (((cdr test-x) '(a b c d)))
  (cdr test-x))

= > (KEY a b c d)

Taking what you say at face value, one might expect that at the moment
the value of (cdr test-x) is printed, it's already been changed back to
the original value, so it should say (KEY 1 2 3 4), but it doesn't. (I
think that in a way, you are in fact correct, but the issue is more
complicated than your formulation suggests; see below.)

> Note that the variable test-x is not shadowed by your letf:
>
> (let ((test-x-before test-x))
>   (letf (((cdr test-x) '(a b c d)))
>     (eq test-x test-x-before)))
>
>==> t

No, it *is* shadowed (more correctly, its cdr is shadowed):

(let ((test-x-before test-x))
  (letf (((cdr test-x) '(a b c d)))
    (cdr test-x-before)))

=> (a b c d)

test-x and test-x-before are `eq`, which means they point to the
(token-)identical object in memory. 

I suspect what is going on is that what is returned by the letf (and
cl-letf) is not the value of the symbol test-x (which I've kinda assumed
up until now), but a pointer to the object test-x is referring to (i.e.,
the first cons cell of the list). If you would shadow test-x itself,
this object is different from the object the global test-x binding
points to, and so that object is printed. (Even though the printing is
done after the letf has returned, since there is still a pointer to the
object, the object itself is kept alive until it's been printed.)

However, in the OP's example, it's not the binding of test-x that is
shadowed, but its cdr. So inside the letf, test-x still points to the
same (token-identical) cons cell as the global binding of test-x, it's
just that that cons cell has a shadowed cdr. What is returned by the
letf is a pointer to that object. In this case, when the letf returns,
there is no longer any pointer to the shadowed cdr, so it is discarded.

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).



-- 
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]