Expression closures - use-cases for shortcut lambda syntax (blocks)

Brad Fults bfults at gmail.com
Fri Mar 16 20:50:31 PDT 2007


I agree that the nested "function () { ... }" syntax gets messy (and
this can be seen especially within complex event-driven scripts), but
trying to define blocks as fundamentally different constructs is, in
my opinion, only more confusing for the ES novice.

I am not inclined to support any of the alternate proposals so far;
"(\" because it is a radical departure from the meaning of '\' in ES
and is the first step on the path to Perl's magic $... madness,
"File.open("file", (file))=>{ ... });" because it is terribly dense
and confusing syntax, and the Ruby-esque "(10).times( |number| { ...
});" because it's yet another nonsensical (and unnecessary) idiom for
the ES coder to learn and remember (though the least confusing of
these three).

>> for (file in autoFile("hello", "r")) {
>>    process(file);
>> }
>>
>
>This is a bit of a side note, but this type of usage of generators can
>be misleading to novice programmers, so I'm not sure if it's any better.

I completely agree -- this is confusing and not explicit in the least
as to what it is actually doing.

I think it's also worth considering that the current (ES3) syntax
allows the novice to mentally step through the process in a logical
manner; e.g. "this is a lambda function that returns a reference to
another lambda function which will inherit scope from the outer lambda
and form a closure". Certainly in the use case of event-driven web
scripts when preparing an event handler, it makes perfect sense that
one would need to call a lambda to return a reference to a new lambda
with a closure to be passed to the browser's event handling system. It
is indirect and complex, but it is not obscure or magical.

I think the only sensible alternative if "typing seven characters" is
a serious issue for the spec to handle is to offer a shortened
function identifier that doesn't change terminology to "blocks" or the
basic ES function syntax. Changing "function" to "fn" comes to mind.
Of course this will be another trade-off with backwards compatibility:
there will always be breakage of older scripts if a new identifier is
introduced.

This would make one of the above examples:

(10).times( fn(number) { ... });

It's shorter, but I'm not convinced it's worth the negatives. You can
bind "function(...) {...}" to a single key in any decent
coding-centric text editor and mitigate the density of syntax with
liberal use of newlines and indentation.

Thanks.

-- 
Brad Fults



More information about the Es4-discuss mailing list