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=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d4b510aac9184a29 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1995-01-18 04:39:41 PST Newsgroups: comp.lang.ada Path: nntp.gmd.de!Germany.EU.net!howland.reston.ans.net!news.moneng.mei.com!uwm.edu!news.alpha.net!news.mathworks.com!uhog.mit.edu!news.mtholyoke.edu!world!bobduff From: bobduff@world.std.com (Robert A Duff) Subject: Re: Ada95 and Dynamic Type Identification Message-ID: Organization: The World Public Access UNIX, Brookline, MA References: Date: Wed, 18 Jan 1995 12:39:41 GMT Date: 1995-01-18T12:39:41+00:00 List-Id: In article , John DiCamillo 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