5853Re: "ocaml_beginners":: object finalization
- May 2, 2006Answering to the following Florent Monnier's question:
>> I would like that a method of an object to be called when this object isRichard Jones wrote:
>> finalized. Is it possible ?
> let () =I am discovering the Gc module thanks to your message. In the OCaml
> let obj = object (self)
> method hello = prerr_endline "hello"
> method finaliser _ = prerr_endline "in finaliser"
> prerr_endline "in initializer";
> Gc.finalise self#finaliser self
> end in
documentation, a warning says that the registered callback has to be
out of the scope of the considered value, such that its closure
doesn't capture the value. Your code could be rewritten as:
let finaliser obj = obj#finaliser
let obj = object (self)
method hello = prerr_endline "hello"
method finaliser _ = prerr_endline "in finaliser"
prerr_endline "in initializer";
Gc.finalise finaliser self
That seems to work.
- << Previous post in topic Next post in topic >>