[plt-scheme] finally

From: Dave Herman (dherman at ccs.neu.edu)
Date: Tue Dec 20 00:14:45 EST 2005

Dave Herman wrote:
> Just for fun, here's a little Java-like "try-catch-finally" facility. 
> Warning: utterly untested.

Here's a more concise version. I'd forgotten about syntax-case*.

   (define-for-syntax (symbolic-identifier=? id1 id2)
     (eq? (syntax-object->datum id1)
          (syntax-object->datum id2)))

   (define-syntax (try stx)
     (syntax-case* stx (catch finally) symbolic-identifier=?
       [(_ e (catch ([pred handler] ...)) (finally e0 e1 ...))
        #'(dynamic-wind void
                        (lambda ()
                          (with-handlers ([pred handler] ...)
                            e))
                        (lambda () e0 e1 ...))]
       [(_ e (catch ([pred handler] ...)))
        #'(with-handlers ([pred handler] ...) e)]
       [(_ e (finally e0 e1 ...))
        #'(dynamic-wind void
                        (lambda () e)
                        (lambda () e0 e1 ...))]))

Dave


Posted on the users mailing list.