From: Joost Kremers <joost.m.kremers@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: How does letf work?
Date: 29 Jan 2014 08:37:45 GMT [thread overview]
Message-ID: <slrnlehfeo.6e2.joost.m.kremers@j.kremers4.news.arnhem.chello.nl> (raw)
In-Reply-To: mailman.13075.1390962244.10748.help-gnu-emacs@gnu.org
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)
next prev parent reply other threads:[~2014-01-29 8:37 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-28 23:10 How does letf work? Florian Beck
2014-01-29 2:23 ` Michael Heerdegen
[not found] ` <mailman.13075.1390962244.10748.help-gnu-emacs@gnu.org>
2014-01-29 8:37 ` Joost Kremers [this message]
2014-01-29 9:14 ` Joost Kremers
2014-01-29 15:29 ` Florian Beck
2014-01-29 16:12 ` Nicolas Richard
2014-01-29 20:19 ` Florian Beck
2014-01-29 15:06 ` Nicolas Richard
2014-01-29 23:46 ` Michael Heerdegen
2014-01-29 23:53 ` Michael Heerdegen
[not found] <mailman.13065.1390951154.10748.help-gnu-emacs@gnu.org>
2014-01-29 0:35 ` Emanuel Berg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=slrnlehfeo.6e2.joost.m.kremers@j.kremers4.news.arnhem.chello.nl \
--to=joost.m.kremers@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.