From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA27175; Fri, 29 Aug 2003 16:33:30 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA27858 for ; Fri, 29 Aug 2003 16:33:29 +0200 (MET DST) Received: from kraid.nerim.net (smtp-105-friday.nerim.net [62.4.16.105]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h7TEXSf17541 for ; Fri, 29 Aug 2003 16:33:28 +0200 (MET DST) Received: from karryall.dnsalias.org (karryall.dnsalias.org [62.4.18.180]) by kraid.nerim.net (Postfix) with ESMTP id 3217640E67; Fri, 29 Aug 2003 15:40:23 +0200 (CEST) Received: by karryall.dnsalias.org (Postfix, from userid 500) id A6F3E1A0685; Fri, 29 Aug 2003 15:40:20 +0200 (CEST) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <16207.22468.534018.193998@karryall.dnsalias.org> Date: Fri, 29 Aug 2003 15:40:20 +0200 From: Olivier Andrieu To: Richard Jones Cc: caml-list@inria.fr Subject: Re: [Caml-list] Polymorphic graph widget problem In-Reply-To: <20030829115446.GA879@redhat.com> References: <20030829115446.GA879@redhat.com> X-Mailer: VM 7.17 under Emacs 21.2.1 From: Olivier Andrieu X-Loop: caml-list@inria.fr X-Spam: no; 0.00; andrieu:01 andrieu:01 caml-list:01 widget:01 widget:01 lablgtk:01 plots:01 gobj:01 generalise:01 gobj:01 val:01 int':01 bool:01 bool:01 ints:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hi, Richard Richard Jones [Friday 29 August 2003] : > As part of a project I'm doing at the moment, I've written a Gtk graph > widget using lablgtk. The graph currently plots ints, so the type > looks something like this: > > class chart : > ?width:int -> > ?height:int -> > ?packing:(GObj.widget -> unit) -> > ?show:bool -> > int array -> (* the data being plotted *) > object > method repaint : Gdk.Rectangle.t option -> unit > end > > All fine, but now I'd like to generalise this so it can plot floating > point values as well as int, thus: > > class ['a] chart : > ?width:int -> > ?height:int -> > ?packing:(GObj.widget -> unit) -> > ?show:bool -> > 'a array -> (* the data being plotted *) > object > method repaint : Gdk.Rectangle.t option -> unit > end first, you do not need to parametrize your `chart' class if the type variable doesn't appear in a method signature. I.e. you can do : class chart : 'a array -> object method repaint : ... end or even : class chart : 'a array -> object val data : 'a array method repaint : ... end > The problem with this is that at various places in the implementation > we need to break the polymorphism. eg. To plot Y labels we call > 'string_of_int', and to work out the height of the Y axis we do some > sums on the values using the (+) operator. Here's one solution : class virtual chart ?width ?height ?packing ?show (data : 'a array) = object (self) method private virtual sum : 'a array -> 'a method private virtual to_string : 'a -> string method repaint = (* code using self#to_string and self#sum *) end class type chart_t = object method repaint : Gdk.Rectangle.t option -> unit end class int_chart ?w ?h ?p ?s data : chart_t = object inherit chart ?w ?h ?p ?s data method private to_string = string_of_int method private sum a = Array.fold_left (+) 0 a end class float_chart ?w ?h ?p ?s data : chart_t = object inherit chart ?w ?h ?p ?s data method private to_string = string_of_float method private sum a = Array.fold_left (+.) 0. a end -- Olivier ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners