GNU bug report logs - #66940
Dynamic scoping is all weird now?

Previous Next

Package: emacs;

Reported by: Dave Goel <deego3 <at> gmail.com>

Date: Sun, 5 Nov 2023 04:08:02 UTC

Severity: normal

Tags: notabug

Done: Michael Heerdegen <michael_heerdegen <at> web.de>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: help-debbugs <at> gnu.org (GNU bug Tracking System)
To: Michael Heerdegen <michael_heerdegen <at> web.de>
Cc: tracker <at> debbugs.gnu.org
Subject: bug#66940: closed (Dynamic scoping is all weird now?)
Date: Wed, 08 Nov 2023 03:09:02 +0000
[Message part 1 (text/plain, inline)]
Your message dated Wed, 08 Nov 2023 04:08:10 +0100
with message-id <87fs1h7xid.fsf <at> web.de>
and subject line Re: bug#66940: Dynamic scoping is all weird now?
has caused the debbugs.gnu.org bug report #66940,
regarding Dynamic scoping is all weird now?
to be marked as done.

(If you believe you have received this mail in error, please contact
help-debbugs <at> gnu.org.)


-- 
66940: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=66940
GNU Bug Tracking System
Contact help-debbugs <at> gnu.org with problems
[Message part 2 (message/rfc822, inline)]
From: Dave Goel <deego3 <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org, Dave Goel <deego3 <at> gmail.com>
Subject: Dynamic scoping is all weird now?
Date: Sun, 5 Nov 2023 00:06:49 -0400
[Message part 3 (text/plain, inline)]
I was playing around, checking my sanity re : dynamic scoping.  I'm
probably rusty, but I could swear that on older emacsen, this sort of code
would print 33 ten times.


On emacs -Q with debian stable (28.2).
I try this


(progn
  (setq lexical-binding nil)

  (dotimes (ii 10)
    (defmacro mac ()
      `(message "%S" ,ii)
      (sit-for 0.1))
    (let ((old_ii ii))
      (setq ii 33)
      (mac)
      (setq ii old_ii)
      )))

Exits without error the first two times, although, strangely, i don't see
any messages the second time. (I expected to see ten 33's).


*(a) As in, the second time, it doesn't print 33 ten times, as we would
expect.*
*(b) The third time is stranger. *
Let's evaluate it a third time! The third time is even weirder.
The third time, it complains
Debugger entered--Lisp error: (void-variable ii)
  (list 'message "%S" ii)
  (lambda nil (list 'message "%S" ii) (list 'print ii) (sit-for 0.1))()
  macroexpand((mac) nil)
  macroexp-macroexpand((mac) nil)
  macroexp--expand-all((mac))
  macroexp--all-forms(((setq ii 33) (mac) (setq ii old_ii)))
  macroexp--expand-all((let ((old_ii ii)) (setq ii 33) (mac) (setq ii
old_ii)))

and so on.


The bug(?) was reproduced on 29.1 as well - by e1f on #emacs

*(c) It gets even funnier. e1f changed ii to var, but the third time, it
still complained about ii, with the above error message, and not about var.*
I reproduced that as well.

Dave
[Message part 4 (text/html, inline)]
[Message part 5 (message/rfc822, inline)]
From: Michael Heerdegen <michael_heerdegen <at> web.de>
To: Dave Goel <deego3 <at> gmail.com>
Cc: Gerd Möllmann <gerd.moellmann <at> gmail.com>,
 66940-done <at> debbugs.gnu.org
Subject: Re: bug#66940: Dynamic scoping is all weird now?
Date: Wed, 08 Nov 2023 04:08:10 +0100
Dave Goel <deego3 <at> gmail.com> writes:

> Thank you

Then let's close this one.

What I still wanted to say (to you): no need to complicate things using
macros here in your example.  Use functions, then variable references
are simple and clear.

If a macro (expansion) needs to refer to run-time values, use macro
arguments to pass an appropriate expression (like a variable).

But this is overkill in your case, no macros needed.  The purpose of
Lisp macros is to extend the language and/or rewrite code (at compile
time).  More or less, only that.  Always think twice if you really need
macros, try to avoid using macros when possible.  This will make the
code easier to understand and maintain.  Only if a macro introduces an
abstraction that makes the code actually easier to understand justifies
using one.

Michael.


This bug report was last modified 1 year and 199 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.