Dear ocamlers,
Xavier Leroy a écrit:
> You can write your example just like in Java, by 1- making the
> instance variable mutable, and 2- using an option type to play the
> role of Java's null pointers. You'd get:
>
> class a arg = object(self)
> val mutable ob = (None : b option)
> initializer ob <- Some(arg self)
> end;;
>
> It's not pretty, but that's exactly what's going on "under the hood"
> in the equivalent Java code.
>
> - Xavier Leroy
An alternative technique I often use is
let not_ready () = failwith "not ready"
class a arg = object (self)
val mutable ob = not_ready
initializer let it = arg self in ob <- (fun () -> it)
method private ob = ob ()
end
the advantage is that it is not necessary anymore to match ob with Some x;
the drawback is that 'ob' value must now be accessed as 'ob ()'.
A workaround is to introduce method ob and type 'self#ob'.
Of course, field ob must not be accessed during (arg self) computation :-(
Cheers.
This archive was generated by hypermail 2b29 : Fri Mar 17 2000 - 11:32:39 MET