comp.lang.ada
 help / color / mirror / Atom feed
From: Bernd Holzmueller <holzmuel@informatik.uni-stuttgart.de>
Subject: Re: Seemingly wierd conversion for in out parameter
Date: 1998/02/26
Date: 1998-02-26T00:00:00+00:00	[thread overview]
Message-ID: <34F58B0B.6345@informatik.uni-stuttgart.de> (raw)
In-Reply-To: 6d02p0$r12$2@plug.news.pipex.net


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
-------------------------------------------------------------------




  reply	other threads:[~1998-02-26  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-02-20  0:00 Seemingly wierd conversion for in out parameter Corey Minyard
1998-02-23  0:00 ` Albert K. Lee
1998-02-23  0:00   ` Corey Minyard
1998-02-25  0:00     ` Nick Roberts
1998-02-26  0:00       ` Bernd Holzmueller [this message]
1998-02-26  0:00         ` Stephen Leake
1998-02-26  0:00           ` Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox