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,8e7f9bcdc1a5b9cd X-Google-Attributes: gid103376,public From: Bernd Holzmueller Subject: Re: Seemingly wierd conversion for in out parameter Date: 1998/02/26 Message-ID: <34F58B0B.6345@informatik.uni-stuttgart.de>#1/1 X-Deja-AN: 328856502 Content-Transfer-Encoding: 7bit References: <6d02p0$r12$2@plug.news.pipex.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: University of Stuttgart Newsgroups: comp.lang.ada Date: 1998-02-26T00:00:00+00:00 List-Id: Nick Roberts wrote: > > -- > Corey Minyard asked a question about conversion of tagged types. > -- > > I'm sure I posted an answer to you on this one a while back! Anyway ... > > Your test code, and the output, constitute legal Ada 95. No 'grey area'. > > ... large portion skipped ... > > Tagged types can always be converted towards the root. If you look at the > code, you will see that V1 gets view-converted to Base_Class on the way into > Process: this is OK, because V1 is of type Der1_Class, whose accessed type > is derived from that of Base_Class (i.e. it accesses Der1, which is derived > from Base, which Base_Class accesses). Then, a value of type Der2_Class is > passed out, which gets view-converted to type Base_Class: this is also OK, > because Der2 is also (indirectly) derived from Base. So the conversion never > violates any Ada rule. If no Ada rule is violated in the program presented by Corey Minyard, they should perhaps be reconsidered as the following, slightly extended program shows: with Text_IO; use Text_IO; with Ada.Tags; use Ada.Tags; ------------------------------------------------------------------------ procedure Tester is type Base is abstract tagged null record; type Base_Class is access all Base'Class; type Der1 is new Base with record X: Integer; end record; type Der1_Class is access all Der1'Class; type Der2 is new Base with record Y: Float; end record; type Der2_Class is access all Der2'Class; procedure Process (Var : in out Base_Class) is begin Var := new Der2'(Y => 3.45); end Process; V1 : Der1_Class := new Der1; -- This will be a compile error. -- U1 : Der1_Class := new Der2; begin Put_Line("Base tag = " & Expanded_Name(Base'Tag)); Put_Line("Der1 tag = " & Expanded_Name(Der1'Tag)); Put_Line("Der2 tag = " & Expanded_Name(Der2'Tag)); Put_Line("V1 tag = " & Expanded_Name(V1'Tag)); V1.X := 6; Process(Base_Class(V1)); Put_Line("V1 tag = " & Expanded_Name(V1'Tag)); Text_Io.Put_Line("V1.X = " & Integer'IMAGE(V1.X)); end Tester; This program outputs Base tag = TESTER.BASE Der1 tag = TESTER.DER1 Der2 tag = TESTER.DER2 V1 tag = TESTER.DER1 V1 tag = TESTER.DER2 V1.X = 1079823565 and the last line of the output is a typical output of an uninitialized variable which is rather strange for a program that has all objects properly initialized! Bernd ------------------------------------------------------------------- Bernd Holzmueller, Computer Science Department, Stuttgart University holzmuel@informatik.uni-stuttgart.de http://www.informatik.uni-stuttgart.de/ifi/ps/bernd.html -------------------------------------------------------------------