From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Real-life examples of lexical binding in Emacs Lisp
Date: Fri, 29 May 2015 14:28:13 +0200 [thread overview]
Message-ID: <87twuv7ele.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.3883.1432888152.904.help-gnu-emacs@gnu.org
Marcin Borkowski <mbork@mbork.pl> writes:
> Hi all,
>
> I googled a bit, and could not find /real-world/ examples of using
> lexical binding and its advantages /in Emacs Lisp/. I understand that
> it's a nice thing to be able to create closures, and that lexical
> binding is in general faster than dynamic binding (which is a bonus in
> itself), but could anyone show me a real /text editing/ problem that
> lexical binding solves, like something that is easier done with
> l.b. than with d.b.? (Examples of general-purpose programming problems
> made easier with l.b. are more or less obvious/easy to find, but Emacs
> is a text editor, after all, and this is its primary area.)
Lexical binding matters for two things:
- it allows the creation of closures.
- it prevents the clobbering of variables.
Closures:
A typical example, is visible in the thread "~`symbol-function' to
get code as list even when byte-compiled?":
;;;; -*- mode:emacs-lisp;lexical-binding:t;coding:utf-8 -*-
(defun add-one-shot-meat (hook fun)
(let ((name (gensym)))
(setf (symbol-function name)
(lambda ()
(remove-hook hook name)
(funcall fun)))
(add-hook hook name)))
Without lexical binding, fun and hook would be dynamic, and
therefore their bindings would disappear when add-one-shot-meat
returns. Therefore they would be undefined variable when the
function is called, or worse, they may be bound at that time by some
other function to something different.
Compare:
(setf lexical-binding t)
(defun e (f)
(let ((v 42))
(funcall f)))
(let ((v 33))
(e (lambda () v)))
--> 33
(setf lexical-binding nil)
(defun e (f)
(let ((v 42))
(funcall f)))
(let ((v 33))
(e (lambda () v)))
--> 42
Clobering variables:
For example, if we have a package such as:
(setf lexical-binding nil)
(defun d () v)
(defun e (f)
(let ((v 42))
(funcall f)))
and we used it with a function in another package such as:
(defun h ()
(let ((v 33))
(d)))
we obtain:
(e (function h))
--> 33
instead of the expected 42.
Hence the workaround of prefixing all variables by the package name,
but this is often insufficient (because package names being often
generic, a different package may name its internal variables
similarly) and not always applied, notably for internal variables.
To be 100% safe without lexical binding, you would have to prefix
ALL your variables with very long package and function name
prefixes.
--
__Pascal Bourguignon__ http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk
next prev parent reply other threads:[~2015-05-29 12:28 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <mailman.3883.1432888152.904.help-gnu-emacs@gnu.org>
2015-05-29 9:30 ` Real-life examples of lexical binding in Emacs Lisp Joost Kremers
2015-05-29 11:12 ` Andreas Röhler
2015-05-29 12:13 ` Dmitry Gutov
2015-05-29 16:21 ` Phillip Lord
2015-05-29 16:50 ` Yuri Khan
2015-05-29 12:28 ` Pascal J. Bourguignon [this message]
2015-05-29 17:16 ` Andreas Röhler
2015-05-29 18:43 ` Emanuel Berg
2015-05-30 5:49 ` Rusi
2015-05-30 12:50 ` Pascal J. Bourguignon
2015-05-30 15:23 ` Rusi
2015-05-30 15:50 ` Pascal J. Bourguignon
2015-05-30 16:21 ` Rusi
2015-05-30 16:03 ` Emanuel Berg
2015-05-30 16:32 ` Rusi
2015-05-30 16:54 ` Pascal J. Bourguignon
2015-05-30 17:10 ` Rusi
2015-05-30 19:12 ` Pascal J. Bourguignon
2015-05-29 8:28 Marcin Borkowski
2015-05-30 8:28 ` Tassilo Horn
2015-06-14 10:52 ` Marcin Borkowski
[not found] ` <mailman.4976.1434279182.904.help-gnu-emacs@gnu.org>
2015-06-14 11:31 ` Pascal J. Bourguignon
2015-06-16 23:48 ` Jim Diamond
2015-06-17 0:06 ` Emanuel Berg
2015-06-17 6:23 ` Andreas Röhler
[not found] ` <mailman.5136.1434522217.904.help-gnu-emacs@gnu.org>
2015-06-17 10:49 ` Pascal J. Bourguignon
2015-06-17 10:53 ` Pascal J. Bourguignon
2015-06-17 14:42 ` Stefan Monnier
2015-06-17 16:19 ` Andreas Röhler
2015-06-17 19:30 ` Tassilo Horn
[not found] ` <mailman.5171.1434557990.904.help-gnu-emacs@gnu.org>
2015-06-17 17:12 ` Stefan Monnier
2015-06-17 20:22 ` Emanuel Berg
2015-06-17 22:13 ` Pascal J. Bourguignon
2015-06-17 23:46 ` Emanuel Berg
2015-06-18 14:57 ` Udyant Wig
2015-06-18 15:47 ` Emanuel Berg
2015-06-19 13:49 ` Udyant Wig
2015-06-17 20:33 ` Emanuel Berg
2015-06-17 22:07 ` Robert Thorpe
2015-06-17 22:17 ` Pascal J. Bourguignon
2015-06-17 0:43 ` Pascal J. Bourguignon
2015-06-17 16:02 ` Phillip Lord
[not found] ` <mailman.5167.1434556959.904.help-gnu-emacs@gnu.org>
2015-06-23 23:49 ` Jim Diamond
[not found] ` <mailman.3950.1432974543.904.help-gnu-emacs@gnu.org>
2015-05-30 12:59 ` Pascal J. Bourguignon
2015-06-14 10:55 ` Marcin Borkowski
[not found] ` <mailman.4977.1434279342.904.help-gnu-emacs@gnu.org>
2015-06-14 20:04 ` Stefan Monnier
2015-06-14 21:44 ` Pascal J. Bourguignon
2015-06-14 21:49 ` Pascal J. Bourguignon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87twuv7ele.fsf@kuiper.lan.informatimago.com \
--to=pjb@informatimago.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.