[Top][All Lists]

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

Re: DynamicBindingVsLexicalBinding

From: Kai Großjohann
Subject: Re: DynamicBindingVsLexicalBinding
Date: Sun, 13 Oct 2013 15:46:19 +0200
User-agent: Postbox 3.0.8 (Macintosh/20130427)

Andreas Röhler wrote:
> That's interesting, but can hand-over functions also with dynamic binding.
> Do you have a real use-case where lexical-binding is superior?
> Can't see goodies from lexical binding beside a simplification for the
> compiler.

I think the key benefit is that lexical binding allows you to support
closures, and with closures you can do cool things.

In Node.js, you need to use closures for everything, due to its
programming model.  For example, when you query a DB in Node.js, you
call a function passing it the query and another function to process the
results:  queryDb(sql, callback);
Here, it is incredibly useful for "callback" to have access to local
variables at the point where queryDb is called.  (I mean variables that
are local to where queryDb is called.)

Some GUI frameworks allow you to specify a function to be called when
the user presses a button, and here, too, it's nice for the function to
have access to local variables from where the button was created.

In Emacs, you write functions to be called when the user presses a key,
and here the solution is that all the variables that the function needs
are buffer-local.

Of course, with dynamic binding you can do other cool things (you can
let-bind a variable, then call a function which calls a function which
calls yet other functions, and the innermost function will reference the
value you just bound -- so you can pass parameters around without
actually having to mention them on every function call).  So each of the
styles has their own advantage.

I find that the idea that variables declared specially (e.g. with
defvar) can be dynamically bound whereas everything else is lexically
bound by default -- this idea is quite useful.  For if you want to
dynamically bind something to influence another function, you need to
know which variables that other function uses -- and defvar is a good
way to document this.


reply via email to

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