all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Michael Heerdegen <michael_heerdegen@web.de>
To: Dave Goel <deego3@gmail.com>
Cc: "Gerd Möllmann" <gerd.moellmann@gmail.com>, 66940@debbugs.gnu.org
Subject: bug#66940: Dynamic scoping is all weird now?
Date: Tue, 07 Nov 2023 06:59:44 +0100	[thread overview]
Message-ID: <87il6e3xyn.fsf@web.de> (raw)
In-Reply-To: <CAOCW0Dgznbj9wH_rSZAB+XSgiUBds+0VdoVBU=rwuW+yapUhzg@mail.gmail.com> (Dave Goel's message of "Mon, 6 Nov 2023 01:57:05 -0500")

Dave Goel <deego3@gmail.com> writes:

> Let's eval this at the start:
>
> (setq lexical-binding nil)
>
> Eval it, so we are operating in the old world of dynamic scoping.

Ok, let's do this.

> (let ((ii 1))
>     (defmacro mac ()
>       `(print ,ii)
>       )
>     (mac))

So now:

  (symbol-function 'mac)
    ==> (macro . (lambda () (list 'print ii)))

> [...]
> So, let's try this -
>
> (setq ii 3)
> (let ((ii 4))
>   (mac))
>
> It evals to 3, not 4. Per our understanding of dynamic scoping, we
> would have expected it use the innermost ii?  Why does it use the
> global ii?

Remember that macros are not expanded on the fly - they are expanded
once for the complete expression in a separate step _before_ the actual
evaluation.  So:

(setq ii 3)
; ii --> 3

Expansion of the second expression:

(let ((ii 4))
  (mac))

  ~~> (let ((ii 4)) (print 3))
                    ^^^^^^^^^
                 expansion of (mac)

Evaluation:

(let ((ii 4)) (print 3))
; prints "3"


A different thing would be to define

     (defmacro mac ()
       '(print ii))

Then the expansion would still reference the variable.  But you could as
well just write (print ii) then.

To sum up: you are using macros wrong in this case.  Macros are not
expanded at run-time.  It is important to remember that.

Note that not even `cl-macrolet' works like this: the definition and
scope of the defined macros is local, but they are still expanded before
evaluation, so the expansion can't refer to run-time bindings as well.


Michael.





  reply	other threads:[~2023-11-07  5:59 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-05  4:06 bug#66940: Dynamic scoping is all weird now? Dave Goel
2023-11-05  4:09 ` Dave Goel
2023-11-05  6:04   ` Gerd Möllmann
2023-11-05  6:58     ` Dave Goel
2023-11-05  7:25       ` Gerd Möllmann
2023-11-05  8:12         ` Dave Goel
2023-11-05  8:39           ` Gerd Möllmann
2023-11-05 19:07             ` Dave Goel
2023-11-05 20:21               ` Dave Goel
2023-11-06  1:52               ` Michael Heerdegen
2023-11-06  6:25                 ` Gerd Möllmann
2023-11-06 16:49                   ` Drew Adams
2023-11-06  6:57                 ` Dave Goel
2023-11-07  5:59                   ` Michael Heerdegen [this message]
2023-11-08  2:49                     ` Dave Goel
2023-11-08  3:08                       ` Michael Heerdegen
2023-11-08  3:21                         ` Dave Goel

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=87il6e3xyn.fsf@web.de \
    --to=michael_heerdegen@web.de \
    --cc=66940@debbugs.gnu.org \
    --cc=deego3@gmail.com \
    --cc=gerd.moellmann@gmail.com \
    /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.