help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [External] : Re: Lexical vs. dynamic: small examples?


From: Emanuel Berg
Subject: Re: [External] : Re: Lexical vs. dynamic: small examples?
Date: Thu, 26 Aug 2021 01:34:42 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Stefan Monnier via Users list for the GNU Emacs text editor wrote:

>> "slet" for statically scoped `let'! But ... if
>> `lexical-binding' has to be enabled anyway then one can
>> just use `let' anyway?
>
> Not if the var has been declared as dynamically scoped.

You are right! It is the `defvar' that does it!

This means, I've been using `defvar' to shut up the
byte-compiler about variables not known to be defined, and
this has actually made them dynamic?

I wonder if I should remove the defvar and have the
byte-compiler complain all it wants (I thought defvar only
meant one intended to use the variable).

But weren't all globals dynamic, I heard?

So why does it "work" with no defvar, just `setq'?

BTW notice that "slet" produces a warning altho it is `let's
behavior one should be wary of ...

;;; -*- lexical-binding: t -*-
;;;
;;; this file:
;;;   http://user.it.uu.se/~embe8573/emacs-init/geh.el
;;;   https://dataswamp.org/~incal/emacs-init/geh.el

(defmacro slet (bindings &rest body)
  (unless lexical-binding
    (error "`slet' requires `lexical-binding' to be enabled") )
  `(funcall
    (lambda ,(mapcar #'car bindings)
      ,@body)
    ,@(mapcar #'cadr bindings) ))

(defvar a)
(setq a 100)

(defun fun ()
  a)

;; geh.el:21:1: Warning: Lexical argument shadows the dynamic variable a
(slet ((a 1))
      (list a (fun)) ) ; (1 100)

(let ((a 2))
  (list a (fun))) ; (2 2)

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

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