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,3d313337c39c5dd5 X-Google-Attributes: gid103376,public From: Matthew Heaney Subject: Re: run-time type identification Date: 1998/09/03 Message-ID: #1/1 X-Deja-AN: 387484399 Sender: matt@mheaney.ni.net References: <01bdd72e$49ee66b0$f330ea9e@ukp03332> <6sm537$agc$1@emu.cs.rmit.edu.au> NNTP-Posting-Date: Thu, 03 Sep 1998 07:05:57 PDT Newsgroups: comp.lang.ada Date: 1998-09-03T00:00:00+00:00 List-Id: sabljak@cs.rmit.edu.au (Steve Sabljak) writes: > You can do run-time type identification like this > > with Ada.Tags; use Ada.Tags; > with A; use A; > > procedure Tag_Test is > > Generalised : A_Ptr; > Specialised : A2_Ptr; > Specialised_2 : A2_Ptr; > > begin > Specialised := new A2'(X => 1, Y => 2); > Generalised := Specialised; > if Generalised'Tag = A2'Tag then > Specialised_2 := A2_Ptr(Generalised); > end if; > end Tag_Test; I prefer to just use a membership test. To test for membership in the specific type A2: if Generalized.all in A2 then To test for membership in the class-wide type A2'Class: if Generalized.all in A2'Class then Don't bother with the Tag attribute or the package Ada.Tags. You don't really need the tag test to do the type cast. If the actual type of the source object (Generalized.all) isn't in A2'Class, then the type cast "A2_Ptr(Generalized)" will raise Constraint_Error: