local

Ingvar von Schoultz ingvar-v-s at comhem.se
Thu Aug 21 16:19:08 PDT 2008


Jon Zeppieri wrote:
> Just to be clear about this: my comments were directed at the claim
> that "let is the new var."  It seemed that one of the design goals of
> the let *declaration* was that it should be a drop-in replacement for
> var, only with block scoping.  That would require (in Scheme terms)
> letrec*-like semantics.  Given
> 
>    let x = 2, y = x * 2;
> 
> ... the let-bound x would have to be visible to y's initializer.

Yes, as a simple javascripter without Scheme experience, I prefer
that everything above be in the same scope. This is simple, it's
free of intricacies that I feel don't add value. And I'd prefer
that all uses of let follow this rule, everything in the same scope:

     let (x = 2, y = x * 2) {...}

     z = let (x = 2, y = x * 2) y;

>    let fn = function(n) { if (n === 0) return 1; else return n * fn(n - 1); };
> 
> ... the 'fn' on the right hand side would refer to the let-bound value
> (and not to something in the enclosing scope).

Yes indeed. If instead left and right side are in different scopes,
it seems the only "useful" thing that this adds is the ability to
give the same name different meanings in adjacent code. But that's
error-prone. Suppose you're reading someone else's code:

     var a = 0; // Say you have noticed this declaration.
     // Several lines of code.
     for (let a = a; a < 10; a++)
     {   // Several lines of code.
         let x = a; // Have you noticed the change in meaning?
     }
     let y = a; // Are you aware that a is unaffected by the loop?

Having left and right in the same scope is simpler, more intuitive
and more useful, in my view.

> So, here is a suggestion that, I am sure, no one but me will like:
> drop let declarations and *only* include let statements and
> expressions.  Give them let semantics.  Maybe this is only true of me,
> but if let statements were in the language, I would never use let
> declarations, anyway.  The advantage of the statement and expression
> forms is that they make the scope of the let-bound variables explicit.

For very small and simple projects it's great that JavaScript lets
you sprinkle your declarations all over the code, with redundant
repetitions if you like. Block scope should be available also with
this usage pattern, in my view.

However, inside the block of a let statement, and its nested blocks,
all let and var declarations might be forbidden. The idea being
that if you use this construct you want all your declarations at
the top of your blocks, and no surprises inside.

Let statements might also enable throwing an error on undeclared
names.

While we're speaking of radical proposals, here's another one: If
let is used anywhere in a block (any type of block), then forbid
var in that block and all its nested blocks. And if var is used,
forbid let. Allow both only in the global scope.

I think the let/var duality will be a source of much confusion.
Every project will want to use one or the other, not both mixed.

That's one reason why I proposed a {{better block}} with let
disabled inside. One behavior, less confusion.

-- 
Ingvar von Schoultz



More information about the Es-discuss mailing list