Short version:
Javascript closures bind their free variables by reference;
what does that mean for const functions?
Consider extreme cases like this (is this currently permitted?)
var real_f;
const f() { return real_f(); }
This make me wonder what it means for a function to be constant
in Javascript - constant wrt l-values is not constant. Or is "freezing"
of constant functions meant to include de-referencing free variables[*]?
And if freezing would evaluate/de-reference free variables, how
would that interfere with programming patterns? For instance,
it might help for some common patterns
for(var i=0; i<length; i++) {
.. const() { .. i ..} // is this i by value, or by reference?
}
but it would rule out use of const functions in patterns that depend
on free by-reference variables
var counter=0;
function inc() { return counter++; } // could we use const here?
Is such a distinction intended for Javascript [*]? It isn't obvious
to me from the current spec[1].
Claus
[1] http://wiki.ecmascript.org/doku.php?id=strawman:const_functions
[*] Apparently, CPL had two forms of function definitions, one
which takes its free variables by l-value (as in Javascript)
and one which takes its free variables by r-value (which we
have to emulate in Javascript by wrapping the closure in an
immediately applied function, eg, for closures having loop
indices as free variables).
This is discussed, eg, in section 3.4.3 ("Modes of free
variables") of Strachey's lecture notes "Fundamental
concepts in programming languages", International
Summer School in Computer Programming
at Copenhagen in August, 1967
http://scholar.google.de/scholar?cluster=7825706085193370698&hl=de&as_sdt=0&as_vis=1
Btw, this also has the earliest discussion of l- and r-values
I am aware of, including the beginnings of conversion
rules (when to de-reference an l-value, and when to pass
the l-value unchanged, eg, in a conditional expression).
Might be of interest to those thinking about References
in the Javascript spec:
https://mail.mozilla.org/pipermail/es-discuss/2011-April/013692.html
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss