[Updated 26/07/07:now captures less variables.]
There are a bunch of operation that must be executed in pairs, for instance openned channel SHOULD be closed. That is: every call to anon a file should be followed by a subsequent on the openned channel.
Edging towards a solution:
Lispers actually have a neat way atomicity of file descriptor operations. with-open-file
takes the name of the file to and a function working on the file handle, this function should not close the file handle. A first shot would look like:
Although at a first glance this looks ok it will break down if an exception is raised in unwind-protect. We will now introduce a new function from the lisp world.
takes two functions, the second one being a cleanup function. returns the result of running . Whatever happens in , will be called.
can now be coded as:
Wrapping it up:
We now would like to force the usage of our new functions instead of the old ones. We do not want to define a new type of channel and there is no way to 'hide' them from, we can however override the functions we don't want to allow with an abstract type:
This looks like yet another modification one could wish for in OCaml standard library.