comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <Nick.Roberts@dial.pipex.com>
Subject: Re: Seemingly wierd conversion for in out parameter
Date: 1998/02/25
Date: 1998-02-25T00:00:00+00:00	[thread overview]
Message-ID: <6d02p0$r12$2@plug.news.pipex.net> (raw)
In-Reply-To: m2en0uvtoe.fsf@wf-rch.cirr.com


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

The essence of your confusion (I think ;-) lies in the conversion
Base_Class(V1) as an actual parameter in a call to the procedure Process.
This conversion is legal Ada: as with all converted in-out parameters, the
actual variable (V1 in this case (of type Der1_Class)) is converted (to type
Base_Class in this case) on the way in, and then whatever the formal (Var in
this case) has been set to (a value of type Der2_Class in this case) is
converted again (into type Base_Class in this case) on the way out.

It is important to understand that with tagged types these conversions are
'view conversions': they never change the type of the underlying object,
they only ever change the _apparent_ type of the object. Hence, an object's
tag never ever changes during the execution of a program; it is totally
static.

So, when a Der2 object is created (by the allocator ('new')) in Process,
that type (Der2) is, and forever must remain, the type of that object. It's
tag is, and will forever be, TESTER.DER2. View conversions to Der1 or Base
(or anything else) won't ever change its tag or its (underlying) type. Note
that the rules for conversion between access-to-tagged types follow the
rules for conversion between the tagged types themselves.

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.

Having been passed out, the (external image of the) tag of the object (of
type Der2) accessed by the value (of type Der2_Class) which was passed out
of Process is printed out. This is "TESTER.DER2", because the type of the
object is Der2.

Hopefully, this makes some sense!


------------ Nick Roberts
---------- Croydon, UK
-------- Nick.Roberts@dial.pipex.com
------ Voicemail & Fax +44 181-405 1124
---- Proprietor, ThoughtWing Software
-- Independent Software Development Consultant

    The oceans teem with myriad fishes:
    The land and air are with creatures rife.
    The world is filled with endless complexity;
    This is the simple truth of life. [NJR]

  *** Ada: The Language for a Complex World ***








  reply	other threads:[~1998-02-25  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 [this message]
1998-02-26  0:00       ` Bernd Holzmueller
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