Questions tagged [lexical-binding]
26 questions
16
votes
3 answers
Defun inside let with lexical binding gives byte-compile warning "the function is not known to be defined"
I want to get the effect of a static variable by using defun inside of let with lexical binding to create a closure. However, when byte-compiling the file, I get a warning. Am I doing something wrong, or if not, is there a way to suppress this…

Rose Kunkel
- 273
- 1
- 7
7
votes
1 answer
What type of variable binding is setq creating?
What type of variable binding or variable or symbol is setq creating, when the variable hasn't been declared prior setq?
Given following source code:
;; -*- lexical-binding: t; -*-
(defun setq-x ()
(setq x 10))
(defvar y 20)
(setq-x)
(message…

jue
- 4,476
- 8
- 20
7
votes
1 answer
How to make sure no global variables have been created in a piece of lisp code?
I am writing some lisp code and I would like it not to mess with the global variables in the system. I am therefore being very careful to only use variables within the scope of the let special form.
However, as the code gets longer, it becomes…

Ruy
- 787
- 4
- 11
6
votes
1 answer
Global lexical-binding setting?
I've seen ;;; -*- lexical-binding: t; -*- at the top of a file and (setq lexical-binding t) as how to turn on lexical binding for elisp code, but is there a way to tell Emacs to simply always do lexical binding, i.e., in your init file? From what…

147pm
- 2,907
- 1
- 18
- 39
6
votes
1 answer
How to make local function binding for closure?
For example, when defining the natural number sequence stream, I can use
;; -*- lexical-binding: t; -*-
(defun nats (n)
(cons n (lambda () (nats (1+ n)))))
(nats 0)
=> (0 closure ((n . 0) t) nil (nats (1+ n)))
But I want to call simply…

xuchunyang
- 14,302
- 1
- 18
- 39
5
votes
1 answer
About closure creation
After reading this article about readable closures, I check that:
Since closures are byte-code function objects, they print readably.
You can capture an environment in a closure, serialize it, read it
back in, and evaluate it. That’s pretty…

anquegi
- 739
- 5
- 21
3
votes
1 answer
why lexical environment alist ends with a `t'?
(let ((test 233))
(lambda ()))
;; ==> (closure ((test . 233) t)
;; ==> nil)
I am wondering why not simply choose a proper alist, e.g.:
((test . 233)) ; instead of ((test . 233) t)
As far as I know, the latter conses more bytes.
emacs-version:…

shynur
- 4,065
- 1
- 3
- 23
3
votes
0 answers
Using a `defconst' or `defvar' While the Variable Has a Local Binding Sets the Global Binding?
The GNU Emacs Lisp Reference Manual, 12.5 Defining Global Variables:
If you use a defconst or defvar special form while the variable has a local binding (made with let, or a function argument), it sets the local binding rather than the global…

shynur
- 4,065
- 1
- 3
- 23
3
votes
1 answer
Warnings about malformed functions when using lexical-let
I have the following function:
(defun filter-repl-show-after (line-num)
"Show lines after LINE-NUM."
(filter-repl--foreach-line
(lexical-let ((line-num line-num))
(lambda ()
(when (> (line-number-at-pos) line-num)
…

Att Righ
- 725
- 4
- 14
3
votes
1 answer
Can I determine the difference of declaring (and not declaring) a file to use lexical-binding?
I have been trying to understand what difference it makes if you set the file variable lexical-binding to t for a particular source file.
As mentioned in emacswiki that in order to use lexical-bindings in your source files, you specifically have to…

myTerminal
- 417
- 1
- 5
- 15
3
votes
2 answers
byte-compile and lexical-binding
Could someone in the know please help me understand what's going on here:
;; -*- lexical-binding: t; -*-
(let* ((a 0))
(let* ((a 1))
(funcall (byte-compile (lambda () a)))))
;;=> 0
wat? Not only that, but with lexical-binding off this…

zeRusski
- 335
- 1
- 8
3
votes
3 answers
How to avoid use of `lexical-let`
I have a macro that intends to creates a closure:
; -*- lexical-binding: t -*-
(defmacro repro ()
(let ((kmap-sym (gensym "kmap-")))
`(let ((,kmap-sym (make-sparse-keymap)))
(define-key ,kmap-sym "a"
(lambda () (interactive)…

erjoalgo
- 853
- 1
- 5
- 18
2
votes
1 answer
Reliable lexical binding?
How does one reliably create a lexically bound/scoped variable? Consider the following:
;; -*- lexical-binding: t; -*-
(let ((f load-file-name))
(defun foo () f))
Normally f would be lexically bound (to the value of load-file-name at load time),…

Richard Hansen
- 495
- 4
- 15
2
votes
1 answer
How can I enable lexical binding for Elisp code in org-mode?
I have the following code:
#+begin_src elisp
(setq lexical-binding t)
(let ((a 1)) ; binding (1)
(let ((f (lambda () (print a))))
(let ((a 2)) ; binding (2)
(funcall f)))) ; result:…

Searene
- 479
- 2
- 14
2
votes
1 answer
lexical-binding only set at the buffer level?
According to this, it seems to say setting lexical-binding only happens at the individual buffer level, i.e. I could not do (setq lexical-binding t) in my init file and have it apply to all subsequent buffers, right?

147pm
- 2,907
- 1
- 18
- 39