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, WEIRD_QUOTING autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ef17a77490cf84e0 X-Google-Attributes: gid103376,public From: Gene Ouye Subject: Re: Dinamic type checking Date: 1998/08/25 Message-ID: <35E27BCC.CD70F24@rational.please_no_unsolicited_mail.com>#1/1 X-Deja-AN: 384494620 Content-Transfer-Encoding: 7bit References: <35E16F1D.2E41DD75@tech.swh.lv> Content-Type: text/plain; charset=us-ascii Organization: Rational Software Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 1998-08-25T00:00:00+00:00 List-Id: To see if an object is an instance of a tagged type (or descendants of the type), try a membership test, ie: with Ada.Text_Io; use Ada.Text_Io; procedure Test_Membership_1 is type A is tagged null record; type B is new A with null record; B_Inst : B; begin if B_Inst in B then Put_Line ("B_Inst is a ""B"""); end if; if B_Inst in A'Class then Put_Line ("B_Inst is in A'Class"); end if; end Test_Membership_1; But this example is pretty trivial. More interesting is: with Ada.Text_Io; use Ada.Text_Io; procedure Test_Membership_2 is type A is tagged null record; type B is new A with null record; type C is new B with null record; A_Inst : A; B_Inst : B; C_Inst : C; procedure Test_For_B (Item : A'Class) is begin Put (Boolean'Image (Item in B) & ' '); end Test_For_B; procedure Test_For_B_Classwide (Item : A'Class) is begin Put_Line (Boolean'Image (Item in B'Class)); end Test_For_B_Classwide; begin Put ("testing instance of A for B and B'Class: "); Test_For_B (A_Inst); Test_For_B_Classwide (A_Inst); Put ("testing instance of B for B and B'Class: "); Test_For_B (B_Inst); Test_For_B_Classwide (B_Inst); Put ("testing instance of C for B and B'Class: "); Test_For_B (C_Inst); Test_For_B_Classwide (C_Inst); end Test_Membership_2; The attribute X'Class refers to the hierarchy of derived types rooted at X. So asking if "Some_Item in X'Class" is asking if Some_Item is a member of X or any of its subclasses. The procedure Test_For_B prints the Boolean image of the result of the membership test checking if the passed in Item is of type B (only true for B_Inst). The procedure Test_For_B_Classwide prints the Boolean image of the result of the membership test checking if the passed in Item is a member of any of the types in the hierarchy rooted at B (true for B_Inst and C_Inst). Gene Ouye