local

Ingvar von Schoultz ingvar-v-s at comhem.se
Fri Aug 22 07:22:42 PDT 2008


Jon Zeppieri wrote:
>> 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?
> 
> I don't find this confusing, at all.  I think it could become
> confusing if there were redundant declarations of the same variable,
> as allowed by var.

Obviously the confusion occurs only if you miss |let a=a|, not if
you see it. Program maintenance becomes more expensive when you
must painstakingly look for things like |let a=a| everywhere.

> Non-recursive bindings are sometimes useful (particularly for simple
> code generation).

It seems trivial to use different names. I really don't get this.
Is there a shortage of letters?

> After thinking about this a bit more, I don't know what single binding
> semantics to prefer:
> 
> let: closest to the immediate function expression + application
> pattern in common use
> let*: like let, but with left-to-right evaluation
> letrec*: closest to current 'var' behavior

If it's like var, people can re-use their knowledge about var.
Let them spend their learning time on more useful things than
tiny scoping intricacies.

>> 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.
> 
> I *do* know, however, that I don't like this.
> 
> I think you, Ingvar, are the lone champion of redundant variable declarations.

Earlier I declared variables only once, and would sometimes
forget. The bugs cost time. With var on every initialization
the problem disappears.

As a result, the lack of var has become a warning that I notice
very clearly. Do I really want global here? I'd prefer an "outer"
declaration, but have to use the lack of var instead.

> I'm not sure who the champions of var hoisting are.  (In another
> thread, Brendan referred to hoisting as a wart.)

That's me too, but it was combined with things that made hoisting
all but disappear. Unfortunately my explanations were bad, and
the proposal drowned under claims that I was instead proposing
something very different and very unworkable that I never intended,
and persistent attacks against this strawman.

I did learn a lot about how to explain better, so maybe I could
try again and get across this time. The real proposal was beautiful
simplicity both in usage and implementation.

>> Let statements might also enable throwing an error on undeclared
>> names.
> I can't imagine what you have in mind here.

     let (a = 3)
     {   x = 5;
     }

If you put |let (a = 3)| above your block, this might tell the
compiler that you want everything neatly declared at the top,
and therefore want an error thrown at the undeclared x. A strict
mode regarding declarations.

-- 
Ingvar von Schoultz



More information about the Es-discuss mailing list