const initializers: once more unto the breach

Allen Wirfs-Brock allen at wirfs-brock.com
Tue Mar 20 13:46:24 PDT 2012


On Mar 20, 2012, at 1:23 PM, Andy Wingo wrote:

> On Tue 20 Mar 2012 21:13, Allen Wirfs-Brock <allen at wirfs-brock.com> writes:
> 
>> On Mar 20, 2012, at 8:55 AM, Andy Wingo wrote:
>> 
>>    On Tue 20 Mar 2012 16:45, Andy Wingo <wingo at igalia.com> writes:
>> 
>> Your original post doesn't seem to have made it to the discussion list. 
> 
> Odd.  Here's the meat:
> 
>    Const is just a bit strange.  For example:
> 
>      (function (){
>         baz = 20;
>         return baz;
>         const baz = 30;
>         baz = 40;
>         baz = 50;
>       })()
>       => 20
no, throws on baz=20; because assignment to an immutable binding


> 
>    Or:
> 
>      (function (){
>         return function(x) { baz = x; return function() { return baz; } };
>         const baz = 30;
>       })(20)()
>       => 20
the baz = x will throw because of assignment to an immutable binding

In these cases, it is possible to detect the assignment to these consts  at "compile" time and they could be treated as early errors.   However, this isn't possible in all circumstances because of the with statement and non-strict mode evals so the mechanisms we have to have the mechanisms in the spec. for dynamically checking and those mechanism also take care of the statically  detectable cases.

It is an open issue whether an implementation should be permitted/required/forbidden to report the statically detectable cases as early errors.  

> 
>    I don't really get the point of const, to be honest.  It doesn't help
>    implementors, because the compiler already knows if a variable is
>    assigned more than once.
> 
>    It doesn't seem to help programmers, because the delayed initialization
>    with temporal dead zone means that you might have to do just as much
>    mental work to figure out what sets a const var as before.  Seeing
>    "const baz;" is pretty strange.
> 
>    Ignorant suggestion: either disallow assignment to const variables
>    outside an initializer, or remove const from the language.
that's how it is spec'ed
> 
>> bugs.ecmasript.org is probably a better place for reporting these shorts of issues.
> 
> OK, thanks.
> 
> Regards,
> 
> Andy
> -- 
> http://wingolog.org/
> 



More information about the es-discuss mailing list