all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tassilo Horn <tsdh@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: About `setf' with macro call
Date: Wed, 17 Apr 2013 16:06:42 +0200	[thread overview]
Message-ID: <87fvypi74d.fsf@thinkpad.tsdh.de> (raw)
In-Reply-To: CAAF+z6GQAZc+ajx8ny4kRuGFNCvoz5JYRGLPipMycQBRaSfWkA@mail.gmail.com

xfq <xfq.free@gmail.com> writes:

> In (info "(cl) Setf Extensions"), there is a example about using `setf'
> on a macro call:
>
>   (defmacro wrong-order (x y) (list 'aref y x))
>   (setf (wrong-order A B) 17)
>
> I evaluated these two expressions, and debugger entered:
>
>   Debugger entered--Lisp error: (void-variable B)
>     (let* ((v B) (v A)) (aset v v 17))
>     (setf (wrong-order A B) 17)
>     eval((setf (wrong-order A B) 17) nil)
>     eval-last-sexp-1(nil)
>     eval-last-sexp(nil)
>     call-interactively(eval-last-sexp nil nil)
>     command-execute(eval-last-sexp)
>
> I understand that `A' and `B' are two invalid S-expressions here.  But
> I don't understand the `let*' expression in the backtrace.

You need to have a look at the macro expansions to understand the
problem.

--8<---------------cut here---------------start------------->8---
ELISP> (macroexpand '(wrong-order A B))
(aref B A)
ELISP> (macroexpand '(setf (wrong-order A B) 17))
(let*
    ((v B)
     (v A))
  (aset v v 17))
--8<---------------cut here---------------end--------------->8---

Now that explains the let* in the backtrace, but the setf-expansion
looks totally wrong!  v is set to B (the index) and then overridden by A
(the array).  So aset becomes called with the array, the array again in
place where the index should be, and then the value to be set...

Let's try it with an example:

--8<---------------cut here---------------start------------->8---
ELISP> (defvar my-array (make-vector 10 1))
my-array
ELISP> my-array
[1 1 1 1 1 1 1 1 1 1]
ELISP> (setf (aref my-array 4) 3)
3
ELISP> my-array
[1 1 1 1 3 1 1 1 1 1]
ELISP> (setf (wrong-order 4 my-array) 17)
17
ELISP> my-array
[1 1 1 1 17 1 1 1 1 1]
--8<---------------cut here---------------end--------------->8---

Strange.  Although the macro expansion looks broken, it works anyhow?!
Now let's test the macro expansion manually.

--8<---------------cut here---------------start------------->8---
ELISP> (let* ((v 4) (v my-array)) (aset v v 19))
*** Eval error ***  Wrong type argument: integerp, [1 1 1 1 17 1 1 1 1 1]
--8<---------------cut here---------------end--------------->8---

Yes, that's what I expected also from the macro version...

Can anyone explain what's going on here?

Bye,
Tassilo




  reply	other threads:[~2013-04-17 14:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-17 13:21 About `setf' with macro call xfq
2013-04-17 14:06 ` Tassilo Horn [this message]
2013-04-17 15:03   ` Stefan Monnier
2013-04-17 15:14     ` Tassilo Horn
2013-04-20  0:46       ` xfq

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=87fvypi74d.fsf@thinkpad.tsdh.de \
    --to=tsdh@gnu.org \
    --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.