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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,dad94612ff745427 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!news.glorb.com!news2.volia.net!news.banetele.no!uio.no!newsfeed1.funet.fi!newsfeeds.funet.fi!feeder1.news.jippii.net!nntp.inet.fi!inet.fi!newsfeed1.nokia.com!news1.nokia.com!not-for-mail From: rick H Subject: Re: Instantiating private types with discriminants? Newsgroups: comp.lang.ada References: <1147252198.138173.203910@j73g2000cwa.googlegroups.com> Organization: home User-Agent: tin/1.8.1-20060215 ("Mealasta") (UNIX) (CYGWIN_NT-5.1/1.5.19(0.150/4/2) (i686)) Message-ID: Date: Wed, 10 May 2006 15:34:10 GMT NNTP-Posting-Host: 172.26.210.110 X-Complaints-To: newsmaster@nokia.com X-Trace: news1.nokia.com 1147275250 172.26.210.110 (Wed, 10 May 2006 18:34:10 EET DST) NNTP-Posting-Date: Wed, 10 May 2006 18:34:10 EET DST Xref: g2news2.google.com comp.lang.ada:4180 Date: 2006-05-10T15:34:10+00:00 List-Id: Dmitry A. Kazakov wrote: > You should have tried it in the same body. Do it as Ludovic did, using > packages: > > ---------- Simple_Case_Types.ads > package Simple_Case_Types is > type General_T is abstract tagged null record; > procedure Put (Item : in General_T) is abstract; > > type Type_A is new General_T with > record > Data : Integer; > end record; > procedure Put (Item : in Type_A); > > type Type_B is new General_T with > record > Data : Float; > end record; > procedure Put (Item : in Type_B); > > end Simple_Case_Types; > > ---------- Simple_Case_Types.adb > with Ada.Integer_Text_IO; > with Ada.Float_Text_IO; > > package body Simple_Case_Types is > procedure Put (Item : in Type_A) is > begin > Ada.Integer_Text_IO.Put (Item.Data); > end Put; > > procedure Put (Item : in Type_B) is > begin > Ada.Float_Text_IO.Put (Item.Data); > end Put; > > end Simple_Case_Types; > > ----------- Simple_Case.adb > with Simple_Case_Types; use Simple_Case_Types; > > procedure Simple_Case is > type Access_T is access General_T'Class; > Var_A : Access_T; -- could be "new Type_A" or a "new Type_B" > begin > Var_A := new Type_B' (Data => 100.0); > Put (Var_A.all); > > end Simple_Case; > > This will compile as expected. > > BTW, I should use pointers only when needed. Differently to C++, in Ada you > can create class-wide objects on the stack. > I'm quite happy to abandon pointers in preference to the class-wide object approach - in all honestly I didn't know that you could declare variables of a class-wide type - and this is a side-effect of a novice not having "Programming in Ada" open when coding! I've now tried declaring Var_A to be... Var_A : General_T'Class; but gnat insists on initialization, and it's here that I tumble. I can declare Var_A with an initialization... Var_A : Type_B' (Data => 100.0); and then the right Put will work... Put (Var_A); However, I can't work out how to change Var_A dynamically - the compiler says "dynamically tagged expression required" if I do... begin Var_A := Type_A' (Data => 100); Put (Var_A); end;