(Arne Babenhauserheide)
2017-03-24: adjust with to use dynamic-wind. Don’t know how to test this yet. adjust with to use dynamic-wind. Don’t know how to test this yet.
diff --git a/examples/with.w b/examples/with.w
--- a/examples/with.w
+++ b/examples/with.w
@@ -13,27 +13,44 @@ define-module : examples with
import : oop goops
-define : enter thing
+define : enter thing state
. thing
define-generic enter
-define : exit thing
+define : exit thing state
. thing
define-generic exit
define-syntax with
syntax-rules : as
: _ thing as name thunk ...
- let*
- : name : enter thing
- res : begin thunk ...
- exit thing
- . res
+ let
+ : name #f
+ state #f
+ res #f
+ dynamic-wind
+ λ () : set! name : enter thing state
+ λ () : set! res : begin thunk ...
+ . res
+ λ () ; : exit thing state
+ . res
-define-method : exit (thing <port>)
+define-method : enter (thing <port>) state
. "Ensure that a port is always closed at the end of the with-block."
+ when : not : equal? #f state
+ set-port-column! thing (car state)
+ set-port-line! thing (car (cdr state))
+ . thing
+
+define-method : exit (thing <port>) state
+ . "Ensure that a port is always closed at the end of the with-block."
+ set! state : list (port-line thing) (port-column thing)
close-port thing
define : main args
with (open-file "with.w" "r") as port
format #t "~a\n" : read port
+ format #t "~a\n" : read port
+ format #t "~a\n" : read port
+
+ ;; TODO: test-continuation