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 PAA23016; Fri, 29 Aug 2003 15:24:14 +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 PAA17617 for ; Fri, 29 Aug 2003 15:23:02 +0200 (MET DST) Received: from mwinf0302.wanadoo.fr (smtp6.wanadoo.fr [193.252.22.28]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h7TDMqf14413 for ; Fri, 29 Aug 2003 15:22:52 +0200 (MET DST) Received: from debian (ca-bordeaux-16-141.w80-8.abo.wanadoo.fr [80.8.88.141]) by mwinf0302.wanadoo.fr (SMTP Server) with ESMTP id 16C10C00028C for ; Fri, 29 Aug 2003 15:22:49 +0200 (CEST) Received: from moi by debian with local (Exim 3.36 #1 (Debian)) id 19sjEV-0007wW-00 for ; Fri, 29 Aug 2003 15:24:07 +0200 To: caml-list@inria.fr Subject: Re: [Caml-list] Polymorphic graph widget problem References: <20030829115446.GA879@redhat.com> From: Remi Vanicat Mail-Copy-To: never Date: Fri, 29 Aug 2003 15:24:07 +0200 In-Reply-To: <20030829115446.GA879@redhat.com> (Richard Jones's message of "Fri, 29 Aug 2003 12:54:46 +0100") Message-ID: <878ypcy4ug.dlv@wanadoo.fr> User-Agent: Gnus/5.090024 (Oort Gnus v0.24) Emacs/21.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: 8bit X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 widget:01 widget:01 lablgtk:01 plots:01 gobj:01 generalise:01 gobj:01 int':01 descriptor:01 bool:01 bool:01 ints:01 gtk:01 int:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Richard Jones writes: > 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 > > 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. > > What I need is some way to isolate the parts which break the > polymorphism. How do I do that? Well, as defined, your class should work not only on int and real but also on string, Array of file descriptor, or whatever. It seem hard. In fact, you need to give to your class not only the data, but also the function for manipulating those data : class ['a] chart : ?width:int -> ?height:int -> ?packing:(GObj.widget -> unit) -> ?show:bool -> 'a array -> (* the data being plotted *) ('a -> string) -> (* a function to have the string representation of the data *) ('a -> int) -> (* a function to have an aproximation of the data *) object method repaint : Gdk.Rectangle.t option -> unit end (well, you might want to choose some other type but you have the idea) -- Rémi Vanicat vanicat@labri.u-bordeaux.fr http://dept-info.labri.u-bordeaux.fr/~vanicat ------------------- 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