Proposal: opt-out local scoping

Ingvar von Schoultz ingvar-v-s at comhem.se
Thu Aug 28 14:43:26 PDT 2008


Yuh-Ruey Chen wrote:
> Well we're all discussing radical new syntaxes (I'm looking at you,
> Ingvar), I might as well propose my own.

Radical? I find my proposal far less radical than, for example, the
Java classes discussed elsewhere.

With Java classes, a paradigm is chosen for you. My sugar embraces
the many paradigms of JavaScript. You get clear syntax for many
different paradigms.

> One thing about JavaScript that has always really bothered me (and
> probably several of you) is how JavaScript has opt-in local scope,
> rather than opt-out local scope.

It's a very annoying drawback. But despite this I'm not convinced
about your proposal. If instead undeclared names are forbidden, this
is so very helpful in debugging that it trumps any other arrangement.
Although your automatic scoping (with some improvements) would be
useful for many users, I'm not convinced that it's useful enough,
when forbidden undeclared are so much more helpful.

However I'd love to see |outer| (which I prefer over |nonlocal|)
required for every variable outside the current function (not block).
That would be a great debugging help:

     var x, y, z;
     function f()
     {   outer x;
         var a = x;
         var b = outer y;
         var c = y;
         var d = z; // Error.
     }

> So here's the proposal that is backwards compatible: provide a block
> that changes the "default" scoping.
> 
>     var { ... }

The syntax would have to be different, because that looks like the
start of a destructuring assignment.

-- 
Ingvar von Schoultz



More information about the Es-discuss mailing list