polymorphic methods in O'Caml (fwd)

From: David Monniaux (monniaux@clipper.ens.fr)
Date: Thu Jan 22 1998 - 18:57:10 MET


Date: Thu, 22 Jan 1998 18:57:10 +0100 (MET)
From: David Monniaux <monniaux@clipper.ens.fr>
To: Liste CAML <caml-list@inria.fr>
Subject: polymorphic methods in O'Caml (fwd)

  This message is in MIME format. The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.
  Send mail to mime@docserver.cac.washington.edu for more info.

---2139231232-1448368586-885402509=:26842
Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
Content-ID: <Pine.SOL.3.95.980122185537.3400L@corvette>

[ J'aimerais des methodes polymorphes dans le systeme d'objets. ]

Hi all,

I'm currently implementing bindings in O'Caml for gtk+, which is the
graphic toolkit used by the (very nice) painting program GIMP
(http://www.gimp.org) and the GNOME project (GNU graphic environment).

The implementation method is the following:
 
gtk+
 |
very simple C layer of interface
 |
Caml bindings (not safe with respect to the gtk hierarchy of type)
 |
hierarchy of O'Caml classes (provides type safety)

I try to minimize the overhead, so each layer is very simple. Most
functions are simple wrappers over the C functions; only callbacks are a
little more complicated to handle.

The problem I have is the following: I have methods that can be invoked on
container widgets to add a widget into them. As methods can be
polymorphic, and more specifically can't have a parameter of type
#someclass, use of those methods has to include a cast onto the type
"widget". This is inelegant (watch for the (xxx :> widget) expressions in
the sample code included).

Therefore, I'd like polymorphic methods to be implemented (even if it
needs some explicit type declaration). Is there any reason why this
couldn't be done?

-- David

open ObjGtk;;

let window = window_new Gtk.WINDOW_TOPLEVEL in
  ( let vbox = vbox_new false 0 in
      vbox #show();
      window #add (vbox :> widget);
      window #border_width 10;

      ( let label = label_new "Dialog box" in
          label #show();
          vbox #pack_start (label :> widget) false false 0
      );

      ( let hbox = hbox_new false 10 in
          List.iter
            (function title,tip ->
               let button = check_button_new_with_label title in
                 button #set_tip tip;
                 button #show();
                 hbox #pack_start (button :> widget) true false 0;
            ) ["foo", "Let's dance.";
               "bar", "There's a lady who's sure of it.";
               "moo", "May the force be with you !";
               "cow", "Summertime and the living is easy..."];
          hbox #show();
          vbox #pack_start (hbox :> widget) false false 0
      );

      ( let hsep = hseparator_new () in
          hsep #show();
          vbox #pack_start (hsep :> widget) false false 5
      );

      ( let ratio = ref 0.5 and pbar = progress_bar_new () in
          pbar #show();
          pbar #update !ratio;
          Gtk.timeout_add 500 (fun _ ->
            ratio := !ratio +. 0.1;
            if !ratio > 1.0 then ratio := 0.0;
            pbar #update !ratio; true);
          vbox #pack_start (pbar :> widget) false false 5
      );

      ( let hsep = hseparator_new () in
          hsep #show();
          vbox #pack_start (hsep :> widget) false false 5
      );

      ( let quitbox = hbox_new false 0 in
          ( let quit = button_new_with_label "Quit" in
              quit #connect_clicked Gtk.main_quit;
              quit #show();
              quitbox #pack_start (quit :> widget) true false 0
          );
          quitbox #show();
          vbox #pack_start (quitbox :> widget) false false 0
      )
  );
  window #connect_delete_event (fun _ -> Gtk.main_quit (); false);
  window #show ();;

---2139231232-1448368586-885402509=:26842--



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:13 MET