From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,7508aa0d80b8bf66 X-Google-Attributes: gid103376,public From: Richard D Riehle Subject: Re: Inheritance and Polymorphism in Ada !! Date: 1999/10/15 Message-ID: <7u7o36$tv8$1@nntp6.atl.mindspring.net>#1/1 X-Deja-AN: 537153937 References: <7u64k3$l1d$1@hiline.shinbiro.com> <3806DC34.1513E8B1@frqnet.de> Organization: MindSpring Enterprises X-Server-Date: 15 Oct 1999 17:25:58 GMT Newsgroups: comp.lang.ada Date: 1999-10-15T17:25:58+00:00 List-Id: In article <3806DC34.1513E8B1@frqnet.de>, Andreas Winckler wrote: > >Chango Cho schrieb: >> >> Do they exist in Ada? > >Yes, but in my opinion polymorphism is kind of restricted in Ada. There are lots of alternatives to dynamic polymorphism in Ada. Here is a simple package with three simplified examples. package Thingamabobs is -- 1 type Gadget is tagged private; -- 2 type Gadget_Pointer is access all Gadget'Class; -- 3 procedure Display (G : in Gadget); -- 4 type Doohickey is new Gadget with private; -- 5 procedure Display (G : in Doohickey); -- 6 function Get return Gadget'Class; -- 7 private -- 8 type Gadget is tagged record -- 9 Is_On : Boolean := False; -- 10 end record; -- 11 type Doohickey is new Gadget with record -- 12 Spinning : Boolean := False; -- 13 end record; -- 14 end Thingamabobs; -- 15 with the following little test program, with Thingamabobs; -- 1 with Ada.Text_IO; -- 2 with Ada.Integer_Text_IO; -- 3 use Ada; -- 4 procedure Test_Thingamabobs is -- 5 Gadget_Stuff : array (1..2) of -- 6 Thingamabobs.Gadget_Pointer; -- 7 := (1 => new Thingamabobs.Gadget, -- 8 2 => new Thingamabobs.Doohickey); -- 9 Index : Positive; -- 10 begin -- 11 Text_IO.Put("Enter Index Value (1/2) " ); -- 12 Integer_Text_IO.Get(Index); -- 13 Thingamabobs.Display(Gadget_Stuff(Index).all); -- 14 end Test_Thingamabobs; -- 15 Another way to approach this is to declare the classwide type within a declare block and constrain it with a function call, with Thingamabobs; -- 1 procedure Get_Thingamabobs is -- 2 begin -- 3 declare -- 4 Gadget_Stuff : Thingamabobs.Gadget'Class -- 5 := Thingamabobs.Get; -- 6 begin -- 7 Thingamabobs.Display(Gadget_Stuff); -- 8 end; -- 9 end Get_Thingamabobs; -- 10 where the declare block could be placed inside a loop. Alternatively, the declare block could (many say _should_) be promoted to a subprogram call. Finally, we have a really shortened example that illustrates some of the power associated with a classwide function and a dispatching call, with Thingamabobs; -- 1 procedure Abbreviated_Get_Thingamabobs is -- 2 begin -- 3 Thingamabobs.Display(Thingamabobs.Get); -- 4 end Abbreviated_Get_Thingamabobs ; -- 5 Typically, the Get will acquire the data from a file, a data structure, or some other container in which the tag has been preserved. For example, you can create a Stream_IO file in which the tag is persistent. Hope this helps, Richard Riehle http://www.adaworks.com