Commit 4cecd67c authored by Stefan Monnier's avatar Stefan Monnier

* doc/lispref/variables.texi (Converting to Lexical Binding): New section

Extract it from `Using Lexical Binding` and extend it a bit.
parent 3b3274a8
...@@ -934,6 +934,7 @@ Lisp programs. ...@@ -934,6 +934,7 @@ Lisp programs.
* Dynamic Binding Tips:: Avoiding problems with dynamic binding. * Dynamic Binding Tips:: Avoiding problems with dynamic binding.
* Lexical Binding:: A different type of local variable binding. * Lexical Binding:: A different type of local variable binding.
* Using Lexical Binding:: How to enable lexical binding. * Using Lexical Binding:: How to enable lexical binding.
* Converting to Lexical Binding:: Convert existing code to lexical binding.
@end menu @end menu
@node Dynamic Binding @node Dynamic Binding
...@@ -1242,9 +1243,10 @@ for those that are only special in the current lexical scope. ...@@ -1242,9 +1243,10 @@ for those that are only special in the current lexical scope.
@end defun @end defun
The use of a special variable as a formal argument in a function is The use of a special variable as a formal argument in a function is
discouraged. Doing so gives rise to unspecified behavior when lexical not supported.
binding mode is enabled (it may use lexical binding sometimes, and
dynamic binding other times). @node Converting to Lexical Binding
@subsection Converting to Lexical Binding
Converting an Emacs Lisp program to lexical binding is easy. First, Converting an Emacs Lisp program to lexical binding is easy. First,
add a file-local variable setting of @code{lexical-binding} to add a file-local variable setting of @code{lexical-binding} to
...@@ -1264,9 +1266,21 @@ variable. If a non-special variable is bound but not used within a ...@@ -1264,9 +1266,21 @@ variable. If a non-special variable is bound but not used within a
variable. The byte-compiler will also issue a warning if you use a variable. The byte-compiler will also issue a warning if you use a
special variable as a function argument. special variable as a function argument.
(To silence byte-compiler warnings about unused variables, just use A warning about a reference or an assignment to a free variable is
a variable name that starts with an underscore. The byte-compiler usually a clear sign that that variable should be marked as
interprets this as an indication that this is a variable known not to dynamically scoped, so you need to add an appropriate @code{defvar}
before the first use of that variable.
A warning about an unused variable may be a good hint that the
variable was intended to be dynamically scoped (because it is actually
used, but in another function), but it may also be an indication that
the variable is simply really not used and could simply be removed.
So you need to find out which case it is, and based on that, either
add a @code{defvar} or remove the variable altogether. If removal is
not possible or not desirable (typically because it is a formal
argument and that we cannot or don't want to change all the callers),
you can also add a leading underscore to the variable's name to
indicate to the compiler that this is a variable known not to
be used.) be used.)
@node Buffer-Local Variables @node Buffer-Local Variables
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment