Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
typing of a class
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2000-03-17 (10:30)
From: thierry BRAVIER <thierry.bravier@d...>
Subject: Re: typing of a class
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 ()

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 :-(