Subject: Re: Boost::ScopeExit [Re: A "killer" macro]
From: rpw3@rpw3.org (Rob Warnock)
Date: Fri, 14 Sep 2007 00:01:03 -0500
Newsgroups: comp.lang.lisp
Message-ID: <gcSdnTH3arISiHfbnZ2dnUVZ_sGvnZ2d@speakeasy.net>
D Herring  <dherring@at.tentpost.dot.com> wrote:
+---------------
| Rob Warnock wrote:
| > ...[elided]...
| 
| The point of ScopeExit is to localize the cleanup code with what's 
| being cleaned up, and delay evaluation until exit from the entire 
| function.  This trivial example doesn't show any benefit -- a 100-line 
| function body containing several ScopeExits might be another story. 
| In that case, a raw unwind-protect might put 50 lines between the 
| action and its conditional cleanup.
+---------------

Aha! yes, I'd overlooked that part of it. You're correct.

+---------------
| > Slightly easier, actually, since you really don't need the
| > second PROGN, given the syntax of UNWIND-PROTECT.
| 
| The second progn is there to handle multiple :scope-guard clauses. 
+---------------

My point was just that UNWIND-PROTECT already accepts multiple
forms in the cleanup section, so the second PROGN is redundant.
You can just ",@" the list of cleanup forms directly into the
UNWIND-PROTECT being generated.

+---------------
| What's missing is a gensym to auto-generate the "complete" flag
| that conditions the execution of :scope-cleanup clauses.
+---------------

What would be useful is to be able to name each of the ScopeExits,
and then MACROLET a COMMIT macro that takes a name that maps to
the correct LET-bound GENSYM'd boolean. That is, something like:

    (with-scope-exits
       ...stuff...
       (scope-exit (foo)
	  ...stuff-to-do-for-foo-chunk...
	  ...rollback-forms-for-foo-chunk...)
       ...more-stuff...
       (commit foo)
       ...even-more-stuff...)

So at the beginning WITH-SCOPE-EXITS would "(LET ((#:G123 nil) ...) ..."
[where #:G123 is the GENSYM the macro assigned to FOO], and then
the (COMMIT FOO) would expand to (SETF #:G123 T), and then one
of the cleanup forms for the UNWIND-PROTECT would be:

        (unless #:G123
	  ...rollback-forms-for-foo-chunk...)

Is that what you're talking about?


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607