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 ocaml.org.

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?

Hi:

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
http://www.merjis.com/richj/computers/ocaml/tutorial/ch12.shtml)
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 =
da#event#connect#expose
~callback:(fun _ -> self#repaint (); false);
end

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
self.

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?

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - 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?"'