r/programming • • Aug 30 '08

Tour De Babel -- Rant about programming languages (at Amazon)

http://steve.yegge.googlepages.com/tour-de-babel
13 Upvotes

122 comments sorted by

View all comments

Show parent comment

1

u/Smallpaul Sep 01 '08

In Python that would just be:

with file_method(filename) as myfile:
   do_stuff_to(myfile)
   do_other_stuff_to(myfile)

1

u/almkglor Sep 01 '08 edited Sep 01 '08

Out of curiousity, how generic is "with" in Python?

Suppose I have a unwinding process for a function (scheme):

(let ((foo #f))
  (dynamic-wind
    (lambda () (set! foo (constructor)))
    (lambda () (do-stuff-to foo)
               (do-other-stuff-to foo))
    (lambda () (destructor foo))))

Is there a generalized syntax which allows for such a constructor/deconstructor style? (in the case of languages which support full continuations, this also performs cleanup upon exiting via an external continuation; I believe the Ruby finally block also handles this properly)

1

u/Smallpaul Sep 02 '08

Python does not allow a function to take three anonymous blocks except as actual functions. The same is true for Ruby. It has been a long time since I used scheme so I may have misinterpreted the question.

1

u/almkglor Sep 03 '08 edited Sep 03 '08

Err, what I mean is, dynamic-wind ensures that the given three functions are executed in order. In particular, if the middle function exits abnormally (via an error or a continuation call), the last function is still invoked (i.e. cleanup), similar to Ruby finally clauses.

A feature of dynamic-wind is, if a continuation is taken within the middle function, and that continuation is called from outside the dynamic-wind call, then the first function gets called on entry to the continuation. It's this bit which I don't think is present in other languages.

It doesn't have to be functions per se, just a generic way of saying "do this bit before you ever ever enter that bit, and make sure to do this other bit no matter what happened to that bit".