help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: sending function arguments to recursive function calls


From: Drew Adams
Subject: RE: sending function arguments to recursive function calls
Date: Tue, 7 May 2013 07:04:15 -0700

> > The key to the puzzle is this little declaration in the 
> > first comment of the file:
> >
> > ;;; face-remap.el --- Functions for ... -*- lexical-binding: t -*-
> >
> > That `lexical-binding t' tells Emacs that the code in this 
> > file is meant to be understood with the variable
> `lexical-binding' bound to t (locally).
> 
> Thanks for the kind words and the explanation. I do not feel very
> confident about having a function that relies on a file scope setting
> of lexical-binding, which as in this case happens 300 lines earlier.
> Wouldn't it be better to have functions that work whatever the value
> of lexical-binding, if possible?

I agree.

Sometimes, however, you specifically want the function definition to take
advantage of one kind of scoping/binding or the other.

IOW, when it makes no difference, then yes, it would be nice not to be bothered
with the difference. ;-)  When it does make a difference, that is, when you want
it to make a difference, then it should be easy to choose and it should be clear
what is going on.

Emacs Lisp does not particularly make things clear, IMO.  Common Lisp does a
pretty good job of helping lexical and dynamic binding cohabit.  Emacs Lisp does
not do such a great job, IMO.  As Stefan pointed out:

  Reality is that there are 2 Emacs Lisps.  One is the
  dynamically scoped Elisp, and the other is the lexically
  scoped Elisp.  They are very similar, but they are two
  different languages.

> > An alternative to using a lexical binding here would be to 
> > simply use this:
> >  `(lambda () (interactive) (text-scale-adjust (abs ',inc)))
> 
> If this alternative works whatever lexical-binding, it seems superior
> to me. Isn't it?

Yes.  That's my opinion, anyway.  In this case (and in many others), there is no
_logical_ reason to pass a variable and have that variable be looked up
lexically when the lambda is invoked.  All that is needed is the variable's
value at the time the lambda is _constructed_.  Anything else is overkill.  

But logic is not the only thing involved here.  Disadvantages of substituting
the value can include: (a) backquote syntax, or its equivalent, is less readable
and (b) the byte compiler might not be able to compile the lambda in a
performant way.

I'm no expert on Emacs byte compilation, but it's possible that the byte
compiler just punts on a dynamically constructed sexp such as the one above,
even though it might be able to figure out that the result in this case is
straightforward.  Dunno.

Lexical binding was added to Emacs relatively recently.  With time, perhaps it
and dynamic binding will become better roommates.  And certainly, with time,
users will become more used to lexical binding and to looking out for
lexical/dynamic binding gotchas.

For one thing, you will no doubt be reflexively looking for a local-variable
binding declaration in the file, right? ;-)  And you will be looking for free
variables, and when you notice one you will ask yourself whether it will be
evaluated using lexical or dynamic binding.  IOW, you will internalize things
like this help thread.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]