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
Object's constructor cannot access self?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Object's constructor cannot access self?


Am I right in thinking that an object's constructor cannot access the
object through self? Is there a reason for this other than the syntax?

My situation is this. (See: the graph widget at
The rough outline of the widget is:

class graph ?width ?height ?packing ?show array =
(* The initialization code goes here. *)

object (self)
inherit widget vbox#as_widget

method private repaint () =
(* repaint method, requires access to self *)

method init =
~callback:(fun _ -> self#repaint (); false);

Because the repaint method uses values and methods from the object, it
needs to have access to self.

When the object is constructed, I need to tie the expose event signal
to the repaint function. Hence my graph#init method. I cannot put this
in the ordinary initialization code because it also requires access to

Now the problem is that when people use my widget, a common mistake is
to forget to call #init. Even I do this, and it causes the widget not
to display itself, but in a very non-obvious way which makes it very
hard to track down the problem.

How can I move the #init code to the main initialization section to
avoid this bug?


Richard Jones.
Merjis Ltd. - all your business data are belong to you.
'There is a joke about American engineers and French engineers. The
American team brings a prototype to the French team. The French team's
response is: "Well, it works fine in practice; but how will it hold up
in theory?"'