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:40:20 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

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

Indeed, it works with just `setq', and no `defvar'!

Now the byte-compiler instead warns about a reference to
a free variable. Maybe it shouldn't do that and instead warn
when one uses `defvar'?

;;; -*- 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) ))

(setq b 200)

(defun fun ()
  b) ; geh.el:18:3: Warning: reference to free variable ‘b’

(slet ((b 1))
      (list b (fun)) ) ; (1 200)

(let ((b 2))
  (list b (fun))) ; (2 200)

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




reply via email to

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