From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 2 Oct 92 15:50:45 GMT From: haven.umd.edu!darwin.sura.net!Sirius.dfn.de!Urmel.Informatik.RWTH-Aachen. DE!liszt!pk@ames.arc.nasa.gov (Peter Klein) Subject: Re: New Tree Example (was Re: MI - clutchin Message-ID: <1992Oct2.155045.18473@Urmel.Informatik.RWTH-Aachen.DE> List-Id: In article 92Sep18160904@Dr_No.mitre.org, eachus@Dr_No.mitre.org (Robert I. Eac hus) writes: > > Gee, this is getting complex. I'm going to try to break it into >separate threads. (I'll be in Pittsburgh most of next week, so >silence will not construe consent to anything. :-) > I hope you don't mind if I answer this thread first. I'm pretty short of time at the moment, and I think the more fundamental things can be discussed better with this simple example. >In article <1992Sep15.140840.3405@Urmel.Informatik.RWTH-Aachen.DE> pk@rwthi3.i nformatik.rwth-aachen.de (Peter Klein) writes: > > Sorry, but I still don't see how you handle polymorphism with generics. > One last example on this: Suppose you have some container class, let's > say a list. In this list, you want to store objects of different types. > In the implementation of the list, you don't know what types, you don't > even know how many. In fact, you *shouldn't* know this, because it has > nothing to do with the list itself. Now, please, how do you do this with > generics? > > I'll answer the last question first... > > package Mixed_Lists is > > type List is private; > > function Is_Empty(L: List) return Boolean; > function New_List... > -- all the usual operations which don't mention elements. > > generic > type Item is private; > package New_Elements is > procedure Append(L: in out List; E: in Item); > ... > end New_Elements; > > private > ... > end Mixed_Lists; > > -- You can do all this with just a little attention to magic in > -- Ada 83. (Each instance of New_Elements gets a unique tag > -- from Mixed_Lists, and does run-time type checking when ^^^^^^^^^^^^^^^^^^^^^^ > -- removing things from lists by matching tags. Of course, in > -- Ada 9X you can use tagged types to do that automatically.) > Well, this is exactly what's wrong with your example. You loose the ability to treat all elements in the list in a uniform way: Using generics, it is the *client* of the list package who determines how to do certain things with the elements - depending on their actual type. In the polymorphistic approach, the elements *themselves* carry the information about how to do something with them. Think about all the clients of such a list who don't care about the actual type of a given element - they simply demand that a certain method can be applied to all elements in the list. Let's say we have a list client which wants to print the list. It knows that all elements can be printed, but it doesn't care how this is done for the element types in question. Now this is exactly what polymorphism buys you: You can apply a method to an object, and the *object* knows what to do. In your proposal, the *client* always has to know which instantiation he has to use to perform the right action. One important point here: We are talking about *single inheritance* so far. > Having said all that, this is the wrong way round, especially in >Ada 9X. First create the node type, which may just be a placeholder, >then mix in the lists, then derive the various element types from the >node type: > > type Node_Type is tagged record null; end record; > > generic > type Element is tagged private; > package Lists is > type Item is new Element with null; -- (I don't like the syntax either. ) > type List is private; > -- define list operations here. (But make sure they are class wide.) > private > ... > end Lists; > > package Node_Lists is new Lists(Node_Type); > > package Identifiers is > type Identifier_Node is new Node_Lists.Item with private; > -- operations on identifiers go here. > private > ... > end Identifiers; > > -- etc., etc. > Excuse me here; my knowledge of Ada 9X syntax doesn't suffice to understand this example. > So far so good, but now we can go one step further. Since >the abstraction of mixed lists is a generally useful one, and there is >no particular need for the parent list element type to have any >attibutes other than those associated with lists, there is no >particular reason to make the list package a generic, and in fact >there is no reason to use multiple inheritance either. :-) :) Don't think so. Actually, if I follow your advice to keep the attribute of 'listable element' apart from other attributes of the element, this is exactly the place where multiple inheritance comes back into the game again. In this case, I make up a virtual base class listable_element, which defines the 'shape' of an element which can be put into the list. Since this feature of being listable has got nothing to do with other properties of an element, it has to be assumed that some possible list element type also is in some arbitrary class hierarchy. Now, if I want to put elements of an arbitrary type (possibly a subtype of an arbitrary supertype) into my list, I'll make this type a subtype of listable_element also. Which, in general, implies that it inherits from at least two supertypes. Peter --- Peter Klein E-Mail: pk@rwthi3.informatik.rwth-aachen.de Lehrstuhl fuer Informatik III Tel.: +49/241/80-21320 Ahornstrasse 55 Fax.: +49/241/80-21329 RWTH Aachen D-5100 Aachen Germany