On Sun, Apr 17, 2011 at 4:06 AM, Claus Reinke <[email protected]>wrote:

> 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].
>

I Claus, I'm not sure why this isn't obvious in the current spec. Please let
me know what needs to be modified so that it becomes obvious.

That spec has much more modest goals for "const" and defines "const
functions" purely in terms of a simple syntactic expansion. Const functions
capture variables in exactly the same manner as normal functions, since the
variable capture is not altered by the syntactic expansion. This is on
purpose -- it is not the intention to alter the variable capture semantics.
The function is "const" only in that it does not provide mutability beyond
what it explicitly states. All the implicit mutability provided by normal
functions -- properties of the function, properties of the function's
.prototype, and assignability of the function variable itself, are
suppressed. Your example

  var real_f;
  const f() { return real_f(); }

simply expands to

  const f = Object.freeze(function() { return real_f(); });
  Object.freeze(f.prototype);
  var real_f;

or equivalent, where the first two lines are promoted to the beginning of
the block they appear in. (Then there's an additional step about further
hoisting to share identities, but this is in order to enable an optimization
that is not relevant to any of the issues you raise.)


>
> 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
>



-- 
    Cheers,
    --MarkM
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to