3

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 opt for it by setting the local file variable.

However, with all that I tried, I could not find any difference in the behavior of the source code within the file and being able to access variables declared in the file from outside and vice-versa.

Is there a way I can see the difference myself for how using ;;; -*- lexical-binding: t -*- in the first line of my source files enables lexical-binding instead of using dynamic-binding?

1 Answer 1

5

Here's a trivial example:

;; -*- lexical-binding: t; -*-

(defun foo ()
  x)

(let ((x 'y))
  (foo))

Save that file and use M-x load-file to load it, and you will get an error:

let: Symbol’s value as variable is void: x

On account of lexical variable x not being visible within function foo.

Under dynamic binding, foo would see the value y.

... being able to access variables declared in the file ...

Note that if the variables are declared with defvar or defcustom or similar, then they are dynamically bound.

1
  • Thanks, this is exactly what I wanted.
    – myTerminal
    Commented Apr 26, 2019 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.