Expression closures - use-cases for shortcut lambda syntax (blocks)
Igor Bukanov
igor at mir2.org
Fri Mar 16 16:45:25 PDT 2007
On 16/03/07, Vassily Gavrilyak <gavrilyak at gmail.com> wrote:
> Next about readability. My primary concern IS exactly readability, I never
> mind typing additional characters.
> But I want to clearly show the intent in my code. I want to see word
> 'function' when it means exactly function.
> And I want to read 'block' when it means block.
> so
> function someFunction(){ // this is a FUNCTION
> using(File("hello". "r"), (\ file){ // this is a block that uses file.
> process(file);
> }) // file will be closed here.
> }
You do not need function closure here in ES4, use generators instead
(that already works in SpiderMonkey since Firefox 2.0):
for (file in autoFile("hello", "r")) {
process(file);
}
where autoFile is a generator:
function autoFile(name, mode)
{
File f = new File(name, mode)
try {
yield f;
} finally {
f.close();
}
}
That is, using idiom from C# is already available in JS with
generators so it can not be used as an argument for (\) expression.
Regards, Igor
More information about the Es4-discuss
mailing list