From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA17480 for caml-redistribution; Fri, 15 Jan 1999 20:20:15 +0100 (MET) 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 QAA22113 for ; Fri, 15 Jan 1999 16:03:33 +0100 (MET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id QAA28318; Fri, 15 Jan 1999 16:02:42 +0100 (MET) Received: (from vouillon@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA14114; Fri, 15 Jan 1999 16:02:42 +0100 (MET) Message-ID: <19990115160242.08046@pauillac.inria.fr> Date: Fri, 15 Jan 1999 16:02:42 +0100 From: Jerome Vouillon To: Markus Mottl , OCAML Subject: Re: subtyping and inheritance References: <199901111852.TAA00822@miss.wu-wien.ac.at> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.89.1 In-Reply-To: <199901111852.TAA00822@miss.wu-wien.ac.at>; from Markus Mottl on Mon, Jan 11, 1999 at 07:52:57PM +0100 Sender: weis Hello, > My specific problem is: I have a program with classes for "terminal" > and "nonterminal" symbols. Both inherit from a base class "symbol". The > symbol class inherits from a class "ord", which defines a virtual method > "compare". This allows me to compare terminals with terminals and > nonterminals with nonterminals, but it also allows comparison between > symbols (all three classes redefine "compare"). > > Look at this graph: > > ord -> defines virtual method "compare" > | > symbol -> redefines "compare" > / \ > terminal nonterminal -> both redefine "compare" > > But the problem is: I will never be able to compare terminals and > nonterminals with each other, because the appropriate comparison method > as defined in "symbol" has been "forgotten" - it is redefined in all > child classes. Thus, I am not able to coerce them to "symbol". > > I have worked around this with the following inheritance scheme: > > ---- ord ----- -> defines virtual method "compare" > | | > | symbol | -> has a method "compare_symbol" > | / \ | > terminal nonterminal -> both redefine "compare" > > Although it is now possible to coerce terminals and nonterminals > to symbols and have them thus compared via "compare_symbol", I am not > content with this scheme: I would really like to have all methods from > "ord" in "symbol", which all make use of the virtual "compare" method. I think the method "compare" in class "ord" need not be a binary method: its argument type can probably instead be a type parameter of the class. class virtual ['a] ord = object method virtual compare : 'a -> bool (* ... *) end;; Then, the method "compare" can be given the type "symbol -> bool" in class "symbol" (and its subclasses) : class symbol = object (self) inherit [symbol] ord method x = "x" method compare other = self#x = other#x end;; class terminal = object inherit symbol method x = "a" method y = "y" end;; This way, you can compare objects from the class "symbol" and any of its subclasses. # new terminal#compare new symbol;; - : bool = false # new terminal#compare (new terminal :> symbol);; - : bool = true Regards, -- Jerome