Inner functions and outer 'this' (Re: That hash symbol)
Claus Reinke
claus.reinke at talk21.com
Sat Mar 26 02:10:34 PDT 2011
> We spent time yesterday at the TC39 meeting not only on shorter
> syntax but exactly how to support better |this| handling for several
> distinct use-cases: inner functions that want the outer |this|,
> callbacks that want a certain |this|, and object methods that
> want the receiver when called as methods of a given (receiver)
> object (else possibly a default such as the outer function's |this|).
That reminds me of an old solution looking for this problem.
Back in 1976, Klaus Berkling suggested to complement the
lambda calculus with an operator to protect variables from
the nearest enclosing binding [1].
The idea is simply that (lexically scoped) variables usually
are bound to the next enclosing binding of the same name,
while protected (lexically scoped) variables are bound to
the next _outer_ enclosing binding of the same name
(each protection key skips one level of binding, lexically).
If I may use '#' as a placeholder for a suitable protection
key, then this translates to Javascript as
function Outer() {
var x = "outer";
function Inner() {
var x = "inner";
log(x); // "inner"
log(#x); // "outer"
log(##x); // global scope, probably unbound
}
}
I've seen this in action in a functional language based on his
ideas, in the late 80s, and have since been repeatedly surprised
by general unawareness of Berkling's ideas. Variants have been
rediscovered occasionally - git's "HEAD^" is a recent example:
http://www.kernel.org/pub/software/scm/git/docs/gittutorial.html
Of course, applying the idea to Javascript's 'this' is complicated
by dynamic scoping. Personally, I've found it useful to think of
'this' as an implicit, lexically scoped parameter - then I only have
to worry about which applications pass which implicit parameter.
I don't know whether that is sufficient to make Berkling's idea
applicable here, but I wanted to make sure it is known;-)
Claus
http://www.haskellers.com/user/claus
[1] The original report is hard to come by, the idea was also
published again in 1982, items 5/6 in this bibliography:
http://www.informatik.uni-trier.de/~ley/db/indices/a-tree/b/Berkling:Klaus_J=.html
Here is the original report's abstract:
@techreport{Berkling76,
author = {Berkling, K.J.},
title = {{A Symmetric Complement to the Lambda Calculus}},
institution = GMD,
note = {ISF-76-7},
month = {September},
year = 1976,
abstract = {"The calculi of Lambda-conversion" introduced by A.Church
are complemented by a new operator lambda-bar, which is in
some sense the inverse to the lambda-operator. The main
advantage of the complemented system is that variables do
not have to be renamed. Conversely, any renaming of
variables in a formula is possible. Variables may, however,
appear with varied numbers of lambda-bars in front of them.
Implementations of the lambda calculus representation with
the symmetric complement are greatly facilitated.
In particular, a renaming of all variables in a formula to
the same one is possible. Variables are then distinguished
only by the number of preceding lambda-bars. Finally, we
give a four symbol representation of the lambda calculus
based on the above mentioned freedom in renaming. },
topics = {FP - Lambda Calculi}
}
More information about the es-discuss
mailing list