Constant function arguments
On Tue, Aug 9, 2011 at 8:39 AM, David Bruant <david.bruant at labri.fr> wrote:
Hi,
I've been playing around with Rhino recently. It already implements a "const" keyword which sounds close to the const proposal (i'm not strong on the details of these). If I understand well, Rhino uses "Scriptable" objects to store scopes. Constant variables are set as constants in these scriptable objects. So, if I understand well, everything can be set as constant in a scope... expect function arguments. Of course, my point here is not to talk about Rhino, but ECMAScript: Would it make sense to have constant function arguments?
If yes, how should it be declared?
One idea, based on other languages (C, maybe other) could be to do: function f(const a, b){ return a+b; }
Yes. I don't remember discussing this, but this lack simply seems like an obvious oversight in the const proposal that we need to fix. Likewise for "} catch (const err) {".
This syntax currently throws a SyntaxError on latest Firefox, Chrome, Opera, so i'll assume ES3 and 5.
Regarding rationale and motivation for const arguments, i would give the same than for const variables which is to have better expressiveness (which allows to catch some errors earlier and to perform easier static analysis and optimizations)
As for const variables, I don't think there's actually any real benefit for static analysis or optimization, since, in the absence of a nested direct eval operator, the absence of assignment is easy enough to detect. However, the readability and error catching benefits are the same, and equally compelling. It helps the intuitive static analysis performed by human readers.
I've been playing around with Rhino recently. It already implements a "const" keyword which sounds close to the const proposal (i'm not strong on the details of these). If I understand well, Rhino uses "Scriptable" objects to store scopes. Constant variables are set as constants in these scriptable objects. So, if I understand well, everything can be set as constant in a scope... expect function arguments. Of course, my point here is not to talk about Rhino, but ECMAScript: Would it make sense to have constant function arguments?
If yes, how should it be declared?
One idea, based on other languages (C, maybe other) could be to do: function f(const a, b){ return a+b; }
This syntax currently throws a SyntaxError on latest Firefox, Chrome, Opera, so i'll assume ES3 and 5.
Regarding rationale and motivation for const arguments, i would give the same than for const variables which is to have better expressiveness (which allows to catch some errors earlier and to perform easier static analysis and optimizations)