local

Breton Slivka zen at zenpsycho.com
Thu Aug 21 22:10:37 PDT 2008


On Fri, Aug 22, 2008 at 1:21 AM, Peter Michaux <petermichaux at gmail.com> wrote:
> On Thu, Aug 21, 2008 at 6:31 AM, Brendan Eich <brendan at mozilla.org> wrote:
>
>> More helpful would be
>> comments on the utility of let blocks (a.k.a. let statements) and let
>> expressions. Also comparisons to the several let forms in Scheme (Jon
>> Z. posted something a while ago on this topic).

Hello, I am a javascript end-user (and occasional abuser). I've been
following this fascinating conversation, and I figure since Brenden is
polling for comments, I thought I would throw mine in. Let expressions
and let statements seem like they might help speed the writing of some
kinds of code, but it looks to me like they complicate reading code
back. In the case of let statements, as I understand it, the current
proposal, and how let works in firefox is that

 let (x = 2, y = x) {
 print(y);
 }

"desugars" to

{
let x = 2, y = x;
print(y);
}

This makes sense to me, and I could possibly even be convinced that
the first form organizes code a bit better, by keeping all the
variables in one place, and distinct from the code that operates on
those variables.

The let expression on the other hand

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

it spooks me out because it's a language construct that uses
parentheses being used kind of like an operator. Similar things can be
done with the if statement, and that usage can easily lead to bugs.
Javascript 1.8's similar treatment of function spooks me in the same
way- because it suddenly becomes more difficult for me as a reader to
determine what code is part of the function, or the let statement, or
the if statement, and which parts are not. At least if there are { }
braces then it's visually obvious where the effect starts and where it
ends.

Though I suppose for some, saving two keystrokes is important.


>
> I was reading the resulting ticket a few days ago.
>
> http://bugs.ecmascript.org/ticket/375
>
> I think that the let statement
>
> let (x = 2, y = x) {
>  print(y);
> }
>
> should desugar to exactly
>
> (function(x, y) {
>  print(y);
> })(2, x)
>
> Also the let expression
>
> z = let (x = 2, y = x) x + y;
>
> should desugar to exactly
>
> z = (function(x, y) x + y)(2, x);
>


I think that intruducing function semantics to the world of "let"
would cause undue confusion, since what we are essentially talking
about is a drop in replacement for "var", plus some additional syntax
to make block scopes easier to create. As has already been covered by
brendan, functions bring with them a boatload of baggage that I don't
necessarily want in just a simple block scope. This is especially
important considering that javascript closures are actually quite an
advanced subject. It would be much easier to explain the let statement
on its own terms, than to try to explain it in terms of everything
that a javascript function does.

But at the same time, I can agree that I woudln't mind some kind of
syntactic sugar for those function patterns. I just don't think "let"
Is the right sugar. I feel that there could be much less verbose ways
of declaring a function in place than what is currently on offer. It
is quite common to declare a function as a callback, and define it in
place as a parameter to another function, and the current syntax for
doing that is quite bulky.  In addition, the syntax for declaring a
function, and calling it in place is bulky as well. (though for many
of the cases where you'd need to do it, a let statement without the
function baggage would work just as well).

When I saw the notes for javascript 1.8 I was quite excited about
syntactic sugar for function declarations, but was quite dissappointed
by what was delivered.  I've already sounded off about my anxiety from
function (a, b) a+b;, What I was expecting, and would have prefered
was something more along the lines of (a, b){ a+b; }; or even fn(a,
b){ a+b; }; it's really the "function" keyword, which cannot even be
properly aliased, that really gets to me. Especially when I just need
to quickly define a function parameter in place.

Though if I supposed that let statements, or some other construct
implemented the real lambdas that Brendan has been mentioning, I would
be just as happy with those, if it could make my code a bit less
bulky, and more readable.


More information about the Es-discuss mailing list