Finally clauses Tony Garnock-Jones (09 Aug 2002 14:05 UTC)
Re: Finally clauses Dave Mason (09 Aug 2002 14:58 UTC)
Re: Finally clauses Richard Kelsey (09 Aug 2002 23:28 UTC)
Re: Finally clauses Tony Garnock-Jones (12 Aug 2002 11:24 UTC)
Re: Finally clauses Richard Kelsey (13 Aug 2002 00:48 UTC)
Re: Finally clauses Tony Garnock-Jones (13 Aug 2002 17:35 UTC)
Re: Finally clauses Richard Kelsey (15 Aug 2002 01:47 UTC)
Re: Finally clauses Tony Garnock-Jones (15 Aug 2002 11:11 UTC)
Re: Finally clauses bear (15 Aug 2002 15:19 UTC)
Re: Finally clauses sperber@xxxxxx (29 Aug 2002 08:08 UTC)
Re: Finally clauses bear (01 Sep 2002 20:55 UTC)
Re: Finally clauses Richard Kelsey (01 Sep 2002 22:22 UTC)
Re: Finally clauses bear (04 Sep 2002 03:07 UTC)
Re: Finally clauses Richard Kelsey (04 Sep 2002 06:55 UTC)

Re: Finally clauses Richard Kelsey 09 Aug 2002 23:26 UTC

   Date: Fri, 9 Aug 2002 15:10:56 +0100
   From: Tony Garnock-Jones <xxxxxx@eservglobal.com>

   Hi. One feature I find valuable in Java that I miss in C++ (and
   Scheme, for that matter) is the idea of a "finally" clause. Would
   srfi-34 be a sensible place to put one of these - either alongside the
   try clause, on its own, or bound up together with it, as perhaps
   try/finally?

Scheme already has a 'finally' in dynamic-wind.  In Java the
'finally' mechanism is combined with exception handling because
raising an exception is the only way to do a throw.  Scheme has
long had throws, via call-with-current-continuation, and in
R5RS it got dynamic-wind as its version of 'finally'.  What it
doesn't have is exceptions.  With SRFI-34 exceptions and throws
are still separate.  You can raise an exception without doing a
throw, and do a throw without raising an exception.

	   (begin
	     (mutex-lock! m)
	     (finally (mutex-unlock! m)
	       (do-operation-requiring-lock)))

This 'finally' can be defined as:

  (define-syntax finally
    (syntax-rules ()
      ((finally final-exp body . rest)
       (dynamic-wind (lambda () #f)
                     (lambda () body . rest)
                     (lambda () final-exp)))))

Note that your example doesn't work well because someone can
use continuations to throw out of the lock-requiring operation
and then back in again.  You could fix this by doing

   (dynamic-wind (lambda () (mutex-lock! m))
                 (lambda () (do-operation-requiring-lock))
                 (lambda () (mutex-unlock! m)))

but your lock-requiring operation still only gets very weak
protection.  The lock only protects the sections that do not do
throws, and you don't need 'finally' for those parts.

The bottom line is that 'finally' is already there but it doesn't
help as much as you would like.
                                       -Richard Kelsey