[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Real-life examples of lexical binding in Emacs Lisp
From: |
Stefan Monnier |
Subject: |
Re: Real-life examples of lexical binding in Emacs Lisp |
Date: |
Wed, 17 Jun 2015 10:42:01 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) |
> Without lexical binding, to try to get some independence from other
> functions, you would have to PREFIX ALL THE LOCAL VARIABLES WITH
> FUNCTION SPECIFIC PREFIXES!
Actually, not really. The Elisp convention to only use prefixes for
global variables is 99% sufficient. In your example, the problem is
that `some-function' modifies the (presumably global) variable
`scratch-buffer' and that this variable does not have an
appropriate prefix.
The only case where non-prefixed local variables cause problem is when
you introduce higher-order functions, as in:
(defun my-map (f l)
(if l (cons (funcall f (car l)) (my-map f (cdr l)))))
(defun my-function (input)
(let ((l ...))
(my-map (lambda (x) (unless (memq x l) (error "invalid mapping")))
input)))
Suddenly the `l' used inside the lambda will not refer to the `l' nearby
but to the `l' argument of `my-map'. For this reason, some higher-order
functions used to use weird argument names to try and avoid such
name capture. Nowadays they can use lexical scoping and hence choose
their variable names sanely and without fear.
Stefan
- Re: Real-life examples of lexical binding in Emacs Lisp, Marcin Borkowski, 2015/06/14
- Re: Real-life examples of lexical binding in Emacs Lisp, Marcin Borkowski, 2015/06/14
- Re: Real-life examples of lexical binding in Emacs Lisp, Pascal J. Bourguignon, 2015/06/14
- Re: Real-life examples of lexical binding in Emacs Lisp, Jim Diamond, 2015/06/16
- Re: Real-life examples of lexical binding in Emacs Lisp, Emanuel Berg, 2015/06/16
- Re: Real-life examples of lexical binding in Emacs Lisp, Andreas Röhler, 2015/06/17
- Message not available
- Re: Real-life examples of lexical binding in Emacs Lisp, Pascal J. Bourguignon, 2015/06/17
- Re: Real-life examples of lexical binding in Emacs Lisp, Pascal J. Bourguignon, 2015/06/17
- Re: Real-life examples of lexical binding in Emacs Lisp,
Stefan Monnier <=
- Re: Real-life examples of lexical binding in Emacs Lisp, Andreas Röhler, 2015/06/17
- Re: Real-life examples of lexical binding in Emacs Lisp, Tassilo Horn, 2015/06/17
- Message not available
- Re: Real-life examples of lexical binding in Emacs Lisp, Stefan Monnier, 2015/06/17
- Re: Real-life examples of lexical binding in Emacs Lisp, Emanuel Berg, 2015/06/17
- Re: Real-life examples of lexical binding in Emacs Lisp, Pascal J. Bourguignon, 2015/06/17
- Re: Real-life examples of lexical binding in Emacs Lisp, Emanuel Berg, 2015/06/17
- Re: Real-life examples of lexical binding in Emacs Lisp, Udyant Wig, 2015/06/18
- Re: Real-life examples of lexical binding in Emacs Lisp, Emanuel Berg, 2015/06/18
- Re: Real-life examples of lexical binding in Emacs Lisp, Udyant Wig, 2015/06/19
- acronymania (was: Re: Real-life examples of lexical binding in Emacs Lisp), Emanuel Berg, 2015/06/19