From: bobduff@world.std.com (Robert A Duff)
Subject: Re: Ada95 and Dynamic Type Identification
Date: Wed, 18 Jan 1995 12:39:41 GMT
Date: 1995-01-18T12:39:41+00:00 [thread overview]
Message-ID: <D2Lpu6.n0y@world.std.com> (raw)
In-Reply-To: milodD2Ko8z.98w@netcom.com
In article <milodD2Ko8z.98w@netcom.com>,
John DiCamillo <milod@netcom.com> wrote:
>
>Does Ada95 have some sort of dynamic type identification
>equivalent to C++ RTTI or Eiffel's assignment-attempt?
>That is, given a procedure
>
> procedure Process_Alerts(A: Alert'Class) ...
>
>Is there any way to find out precisely what type of
>object A is?
You can say:
if A in This_Type'Class then
This_Type'Class(A).Some_Field := ...;
-- Some_Field is declared in This_Type.
elsif A in That_Type'Class then
...
else
...
end if;
where This_Type and That_Type are descendants of Alert. Or, leave off
the 'Class if you want to check whether A is a member of the specific
types, and not any further descendants of them.
A type conversion from A to This_Type'Class (or This_Type) will
automatically do a run-time check, to make sure A in This_Type'Class (or
This_Type, resp.). The type conversion in the above example can't fail,
and a decent compiler will not generate any code for it.
You can also say "if A'Tag = This_Type'Tag then ...",
which is the same as "if A in This_Type then ...".
However, 'Tag is more useful to check whether two
objects came from the same type, as in:
if Set1'Tag = Set2'Tag then
declare
Set3: Set'Class := Union(Set1, Set2);
...
>I couldn't find any references to this ability in Barnes'
>"Introducing Ada 9X" or the Rationale, but I wasn't sure
>what topic to look for either.
If it's not there (or deeply hidden), it's probably because the *usual*
way of checking the tag of an object is the implicit way that happens
during dispatch. That's true of Eiffel and C++, too. I suspect all of
the above methods are *somewhere* in the Rat (they ought to be),
although they are likely not in the Intro document.
- Bob
next prev parent reply other threads:[~1995-01-18 12:39 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <milodD2Ko8z.98w@netcom.com>
1995-01-18 8:48 ` Ada95 and Dynamic Type Identification Stephane Barbey
1995-01-18 12:39 ` Robert A Duff [this message]
1995-01-18 17:33 ` David Moore
1995-01-18 19:12 ` R. William Beckwith
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox