let/RAII
On Jul 31, 2007, at 12:49 PM, Won wrote:
Greetings (my first post) --
I'm pretty excited about the proposed changes coming up for ECMAscript. "let" in particular seems overdue. For better or worse, I have a C++ programming background, and one of the idioms that I find useful every day is RAII (resource acquisition is initialization), also called the "Dispose" pattern in other languages. When a variable falls out of scope (say, an object that contains a file handle), then a method gets called (e.g. close file handle). In particular, RAII makes exception-safe coding much more convenient.
You can use try-finally already in ES3 for exception-safe cleanup:
let (i) { try { i = getinput(); doit(i); } finally { cleanup(i); } }
But RAII means implicit cleanup on block exit, so this is not enough
-- I just wanted to point it out in case someone on the list new to
the language missed it.
In C++, every object gets this treatment. Other languages have special syntax/forms, such as "using" in C#, "with" in Python and VB, and Ruby/Smalltalk has variables scoped in closure blocks.
The advantage of these approaches is the implicit close or exit
automation.
The only thing on the charts for ES4 that could be used is the
iterator protocol (wiki export at developer.mozilla.org/es4
proposals/iterators_and_generators.html is unfortunately quite out of
date, and we are moving the dokuwiki to ecmascript-lang.org
soon -- look there in a week or so):
function raii() { try { yield getinput(); } finally { cleanup(); } }
for (let i in raii()) { doit(i); }
This looks like a loop, which is confusing compared, e.g., to Python
2.5's with statement. But it has the desired implicit close, if you
squint hard and consider the generator function with its try-yield/
finally the equivalent in explicit declarative overhead to a Python
object that has the necessary metaprogramming hooks (enter,
exit if memory serves), etc. for C++ (auto storage class instance
where the class has a dtor that calls cleanup()).
We are now planning to automate close for all iterators, not just for
generators, when started from a for-in loop, so you could write your
own object implementing the iteration protocol, and avoid the
generator function. But the generator function is probably shortest
and it's built-in.
It wouldn't be hard to sugar this more, but we haven't considered any
proposals in this area. Feel free to make one.
On 2007-07-31, at 17:34 EDT, Brendan Eich wrote:
We are now planning to automate close for all iterators, not just for generators, when started from a for-in loop, so you could write your own object implementing the iteration protocol, and avoid the generator function. But the generator function is probably shortest and it's built-in.
What happens when I call a generator function not in a loop?
If you start it, it's up to you to call close on it. This was all
worked out here in the thread with subject "Immediate closing of
iterators" end of last year and early this year -- you posted a
followup agreeing ;-).
Greetings (my first post) --
I'm pretty excited about the proposed changes coming up for ECMAscript. "let" in particular seems overdue. For better or worse, I have a C++ programming background, and one of the idioms that I find useful every day is RAII (resource acquisition is initialization), also called the "Dispose" pattern in other languages. When a variable falls out of scope (say, an object that contains a file handle), then a method gets called (e.g. close file handle). In particular, RAII makes exception-safe coding much more convenient.
In C++, every object gets this treatment. Other languages have special syntax/forms, such as "using" in C#, "with" in Python and VB, and Ruby/Smalltalk has variables scoped in closure blocks.
Some more info can be found here: www.hackcraft.net/raii
So I skimmed the wiki and the es4-discuss mailing list archives, and didn't notice any references to RAII. Is it possible to extend the behavior of "let" to support RAII?
Thanks! Won