Jon Harrop wrote: >On Monday 28 January 2008 14:23:01 you wrote: > > >>Jon Harrop wrote: >> >> >>>There are also many features that I would like to steal from other >>>languages: >>> >>>. The IDisposable interface from .NET and F#'s "use" bindings. >>> >>> >>Is there a reason that Gc.finalise doesn't work? >> >> > >Absolutely: Gc.finalise is only probabilistic whereas IDisposable is >deterministic. IDisposable guarantees deallocation of resources by a certain >point. (This is why you should never use Gc.finalise alone to manage the >collection of external resources!) > >So you write a "use" binding: > > let read_first_line file = > use ch = open_in file in > input_line ch > >and it gets translated into: > > let read_first_line file = > let ch = open_in file in > try input_line ch finally > ch#dispose > > What happens when I write: let broken file = use ch = open_in file in (fun () -> input_line ch) ? Or some other tricky way to let ch escape the scope? Monads strike me as being a better way to do this, but again, we're talking about deep changes to Ocaml. The alternative- wait until the object is garbage collected, depends upon the form of the garbage collector. Brian