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: Sat, 28 Aug 2021 03:36:53 +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:

> Of course. If the compiler says it's "not known to be
> defined" that means the var isn't bound lexically (either
> that, or there's a bug in the compiler), so it can only make
> sense if the var is global or dynamically scoped (and both
> are closely related).

Check this out, here we seemingly define a global variable
which isn't special/dynamic (because `special-variable-p' says
it isn't) but it behaves like one anyway so I guess it is?
Maybe it (the predicate) can only look at the definition and
if it doesn't have an initial/default value it can't tell
... something?

;;; -*- lexical-binding: t -*-
;;;
;;; this file:
;;;   https://dataswamp.org/~incal/emacs-init/scope.el
;;;
;;; g  = global
;;; sd = special/dynamic
;;; ls = lexical/static
;;; v  = variable

(defvar gsd-v 1)            ; 1
(special-variable-p 'gsd-v) ; t

(defvar gls-v)
(setq gls-v 2)              ; 2
(special-variable-p 'gls-v) ; nil

(defun fun-lol ()
  (list gsd-v gls-v v) )

(setq lexical-binding nil)

(let ((gsd-v 100)
      (gls-v 200)
      (v     300) )
  (list (list gsd-v gls-v v)
        (fun-lol) )) ; ((100 200 300) (100 200 300))

(setq lexical-binding t)

(let ((gsd-v 100)
      (gls-v 200)
      (v     300) )
  (list (list gsd-v gls-v v)
        (fun-lol) )) ; DNC, lexical/static `let' binding v undefined in fun-lol

(defun fun-lol-2 ()
  (list gsd-v gls-v) )

(let ((gsd-v 100)
      (gls-v 200)
      (v     300) )
  (list (list gsd-v gls-v v)
        (fun-lol-2) )) ; ((100 200 300) (100 200))

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




reply via email to

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