Questions tagged [lexical-scoping]

48 questions
32
votes
3 answers

Why is `let' faster with lexical scope?

While reading through the source code for the dolist macro, I ran into the following comment. ;; This is not a reliable test, but it does not matter because both semantics are acceptable, tho one is slightly faster with dynamic scoping and the…
Malabarba
  • 22,878
  • 6
  • 78
  • 163
18
votes
2 answers

Why do setq and set quote act differently on let-bound variables with lexical scope?

I had a bug in one of my extensions that eventually turned out to be caused by set not working as I expected: ;; -*- lexical-binding: t -*- (let ((a nil)) (setq a t) (print a)) (let ((a nil)) (set 'a t) (print a)) when run with emacs -Q…
dshepherd
  • 1,281
  • 6
  • 18
18
votes
1 answer

Dealing with "Warning: assignment to free variable" when certain libraries can be missing by design

Byte compilation of my mode: (defun dict-setup-expansions () "Add `dict-mode' specific expansions." (set (make-local-variable 'er/try-expand-list) (list #'dict-mark-article))) gives warning: Warning: assignment to free variable…
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…
13
votes
1 answer

What are the potential pitfalls of enabling lexical-binding for a buffer?

This was inspired by the discussion of lexical-binding vs lexical-let in this question. As lexical-binding gives you the ability to have useful closures people may be used to in other languages like JavaScript why wouldn't you enabled it all the…
stsquad
  • 4,626
  • 28
  • 45
11
votes
1 answer

How is the variable scoping for macros determined?

Take the following example macro, defined in macro.el. (defmacro some-macro (&rest body) `(let ((some-variable 1)) ,@body)) And take the following function, defined in a different file, function.el. (defun some-function () (some-macro…
Malabarba
  • 22,878
  • 6
  • 78
  • 163
10
votes
3 answers

Why does defvar scoping work differently without an initvalue?

Suppose I have a file named elisp-defvar-test.el containing: ;;; elisp-defvar-test.el --- -*- lexical-binding: t -*- (defvar my-dynamic-var) (defun f1 (x) "Should return X." (let ((my-dynamic-var x)) (f2))) (defun f2 () "Returns the…
Ryan C. Thompson
  • 435
  • 2
  • 10
8
votes
2 answers

'Symbol's value as variable is void' in callback from url-retrieve

When executing the following I get an error: (defun caller (func) (url-retrieve "http://m0smith.freeshell.org/" (lambda (status) (funcall func)))) (caller (lambda() (message "called"))) Result: error in process filter: Symbol's value as…
M Smith
  • 279
  • 1
  • 7
6
votes
2 answers

Why doesn't "complement" work as a function?

I'm trying to implement the complement function from Common Lisp (CL) in Emacs Lisp, but I'm confused by why defining it as a function (as opposed to a macro) does not work. The macro definition is straightforward: (defmacro complement (function) …
Tianxiang Xiong
  • 3,848
  • 16
  • 27
6
votes
1 answer

Scope in lambda

In my .emacs conf file, I've got this function that adds a hook to set the compile-command based on the mode. It looks something like, (defun set-compile-cmd (mode-hook cmd) (add-hook mode-hook (lambda () (set (make-local-variable…
5
votes
2 answers

defun in defun is not local?

I am reading SICP to learn Lisp, the book uses Scheme dialect. I want accommodate it to emacs lisp, I find something weird in 3.1.1 and cannot figure it out, say I have a test.el file: (defun make-account () (defun withdraw (amount) (print "in…
Alaneuler
  • 277
  • 1
  • 8
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…
5
votes
1 answer

void-function lexical-let

I'm using this code on Xubuntu (emacs 24.5.1) but it throw error on windows (emacs 25.0.50.1 64bit) (defun ignore-error-wrapper (fn) "Funtion return new function that ignore errors. The function wraps a function with `ignore-errors' macro." …
jcubic
  • 691
  • 1
  • 4
  • 16
4
votes
1 answer

why is a let binding is ignored in compiled function?

I have this function which works perfectly in an uncompiled function. It should let-bind the variable bibtex-completion-bibliography to the result of (org-ref-find-bibliography). (defun org-ref-valid-keys () "Return a list of valid bibtex…
John Kitchin
  • 11,555
  • 1
  • 19
  • 41
4
votes
3 answers

idiomatic way to lexically scope variables in a cl-loop body

Here is some asynchronous code in a cl-loop: ;;; foo.el --- -*- lexical-binding: t; -*- (let ((my-list '(a b c))) (cl-loop for index below (length my-list) for value = (seq-elt my-list index) do…
Damien Cassou
  • 877
  • 4
  • 14
1
2 3 4