A simple translation of the scoping-block syntax sugar -- Was: Re: function hoisting like var

Ingvar von Schoultz ingvar-v-s at comhem.se
Tue Jul 29 11:38:14 PDT 2008


If the simple translation of {{ }} is used, any governing
for(), while() or switch() must be moved inside the scoping
function.

A very simple, very minimalistic approach may be enough.

     for (var Name in Thing)
     {{
     }}

Name is locally contained inside {{ }} while Thing is outside.
But maybe the compiler doesn't have to make this distinction.

Personally I'd never use the same name with different meanings
so close together, like this:

     var Thing = ...;
     for (var Name in Thing)
     {{  var Thing = ...;
     }}

I find that obscure and error-prone.

If the limitation is acceptable, a very minimalistic translation
of {{ }} could simply move the entire for (...) inside the
scoping function, and leave it at that. As long as Thing isn't
redeclared inside, the outer Thing is visible and it works.

Does this make it simple enough for ES3.1?

As an added bonus, the functionality becomes very easy to explain.
The rules become very plain and simple.

I don't know if a let statement with this limitation would be
useful. Every description of let that I can find re-uses names.
But if it's useful, it could be trivially implemented as syntax
sugar:

     let (a = x, b = y)
     {{
     }}

would be syntax sugar for

     {{  var a = x, b = y;
     }}

However "let" has different semantics in at least one existing
implementation (it does distinguish between same-name variables
in the two scopes), so if a minimalistic let is introduced, it
should probably use a different keyword.

-- 
Ingvar von Schoultz

------- (My quirky use of capitals in code comes from my opinion that
reserved and predefined words should all start with lowercase, and
user-defined should all start with uppercase, because this will easily
and elegantly prevent a host of name-collision problems when things
like programming languages are upgraded with new labels.)



More information about the Es4-discuss mailing list