Expression closures - use-cases for shortcut lambda syntax (blocks)
Neil Mix
nmix at pandora.com
Sat Mar 17 09:10:58 PDT 2007
> Let me see if I understand your proposal.
Bleh. I should have known better.
Let me re-iterate: I'm *not* proposing that syntax. I was (poorly)
illustrating my assertion about the true cause of illegibility in the
use of anonymous functions as parameters.
If someone smarter than me can work up a non-alien syntax for
anonymous functions that reduces the number of parens and curlies,
more power to them. As for me, I'm now regretting that I've confused
this thread so thoroughly, and am distancing myself from the idea
altogether.
Back to the original topic, and speaking of reducing the number of
parens and curlies, one thing I forgot to do in my rewrite of
Robert's mochikit examples was to make use of the new expression
closure capabilities in es4. With those, we can change one of the
more "painful" examples from this:
addLoadEvent(function(){
connect('bad_example','onclick',
function(e){signal
('bad_example','showvalue',"explicit_value")});
connect('bad_example','showvalue',window,'alert');
connect('good_example','onclick',
function(e){signal(randomObj,'showvalue',"explicit_value")});
connect(randomObj,'showvalue',window,'alert');
});
to this:
addLoadEvent(function(){
connect('bad_example','onclick',
function(e) signal
('bad_example','showvalue',"explicit_value"));
connect('bad_example','showvalue',window,'alert');
connect('good_example','onclick',
function(e) signal(randomObj,'showvalue',"explicit_value"));
connect(randomObj,'showvalue',window,'alert');
});
Now, adding in the \( lambda syntax under consideration, we get this:
addLoadEvent(function(){
connect('bad_example','onclick',
\(e) signal('bad_example','showvalue',"explicit_value"));
connect('bad_example','showvalue',window,'alert');
connect('good_example','onclick',
\(e) signal(randomObj,'showvalue',"explicit_value"));
connect(randomObj,'showvalue',window,'alert');
});
I believe there's a correlation between the size of the lambda and
the improved legibility with this shortcut -- the smaller the lambda,
the more helpful the shortcut is. For example, change something like
this:
addLoadEvent(function(){
connect('bad_example','onclick',
function(e){signal('bad_example)});
connect('bad_example','showvalue',window,'alert');
connect('good_example','onclick',
function(e){signal(randomObj)});
connect(randomObj,'showvalue',window,'alert');
});
to this:
addLoadEvent(function(){
connect('bad_example','onclick', \(e) signal('bad_example'));
connect('bad_example','showvalue',window,'alert');
connect('good_example','onclick', \(e) signal(randomObj));
connect(randomObj,'showvalue',window,'alert');
});
I defer to others for thoughts and opinions.
More information about the Es4-discuss
mailing list