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: Nicolas Richard
Subject: Re: How does letf work?
Date: Wed, 29 Jan 2014 16:06:27 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

Joost Kremers <joost.m.kremers@gmail.com> writes:
> 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).

How I understand it is : what is returned by the letf *is* the value of
the symbol, but since it is a cons cell it can be modified by setf
(using setcar and setcdr). So part of the job of letf is to *change* the
cons cell upon exiting. The box is the same (and is what is returned),
but the content was changed at the moment the last closing paren of letf
is crossed. IOW:

(defvar test-x '(KEY 1 2 3 4))

(letf (((cdr test-x) '(a b c d)))
  (copy-sequence  test-x))
=> (KEY a b c d)

but

(copy-sequence
 (letf (((cdr test-x) '(a b c d)))
   test-x))
=> (KEY 1 2 3 4)


Returning (copy-sequence test-x) instead of
test-x will produce the expected value.

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

The object is the same, but its content changed. Similar to :
(let ((foo (cons 'a 'b))
      (bar))
  (setq bar foo) ;; bar and foo have the same object in their value cell.
  (setcdr foo 'c) ;; change the cdr of that cons cell
  (eq bar foo)) ;; they're still the same.
=> t

> 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

I guess it's not gone per se until the garbage collector does its work.

-- 
Nico.



reply via email to

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