[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: run-with-timer does not display message
From: |
Sebastian Wiesner |
Subject: |
Re: run-with-timer does not display message |
Date: |
Sat, 19 Jul 2014 17:09:32 +0200 |
Am 18.07.2014 um 23:34 schrieb Emanuel Berg <embe8573@student.uu.se>:
> Stefan Monnier <monnier@iro.umontreal.ca> writes:
>
>> Please don't quote your lambdas!
>
> Do you mean in that case or never?
>
> I have had problems with lambdas and both parameters
> and `let' bindings.
Enable lexical-binding in your Emacs Lisp files to avoid these.
See
https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html
and
https://www.gnu.org/software/emacs/manual/html_node/elisp/Using-Lexical-Binding.html
for details.
> For example, this works but not without the
> backtick/backquote (and the commas):
With lexical binding it does.
> (defun shortcut-to-file (key-prefix key file-prefix file)
> (global-set-key
> (format "%s%s" key-prefix key)
> `(lambda ()
> (interactive)
> (do-show-file (format "%s%s" ,file-prefix ,file)) )))
With lexical binding it works without any quoting:
(defun shortcut-to-file (key-prefix key file-prefix file)
(global-set-key
(format "%s%s" key-prefix key)
(lambda ()
(interactive)
(do-show-file (format "%s%s" file-prefix file)))))
The arguments are captured in a closure, and thus preserved when the lambda
body is evaluated.
This is more efficient than your variant. Lexical bindings are generally more
efficient than dynamic ones, because local variables can be elided entirely,
and the byte compiler can now inspect and byte-compile the lambda form.
It’s also safer, because the byte compiler can now warn you about unused
variables or free variables, which helps you to catch misspelled variable names.