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 Tony Garnock-Jones 12 Aug 2002 11:29 UTC

> Scheme already has a 'finally' in dynamic-wind.

Yup :-) I realised my mistake at about 3am on Sunday...

However I still think (finally) does something *slightly* different to
what is done by (dynamic-wind): where (dynamic-wind) executes its
third thunk on *any* throw past it, I'd expect (finally) to be
triggered by only either normal return, or exception-throw. That is,
the finally-clause gets installed *as an exception-handler*, not a
general unwinder. Perhaps (apologies for the untested, lowlevel
macro):

(define-macro (finally final-exp body . rest)
  (let ((exn (gensym))
	(result (gensym)))
    `(with-exception-handler
      (lambda (,exn)
	,final-exp
	(raise ,exn)) ; note[1]
      (lambda ()
	(let ((,result (begin
			 ,body
			 ,@rest)))
	  ,final-exp
	  ,result)))))

[1] I'm assuming here (I hope I read the SRFI correctly?) that (raise)
within an exception-handler chains to the next enclosing handler.

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

If someone is using call/cc to implement coroutines, or microthreads,
or what-have-you, this means that this
coroutine/thread/dynamic-context will give up its lock on each context
switch. What I was after was relinquishment of the lock when code
either returned normally, or threw an exception...

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

Well, mostly because it's dynamic-wind, and dynamic-wind doesn't make
any distinctions between various kinds of throw.

Tony