comp.lang.ada
 help / color / mirror / Atom feed
From: Gene Ouye <geneo@rational.please_no_unsolicited_mail.com>
Subject: Re: Dinamic type checking
Date: 1998/08/25
Date: 1998-08-25T00:00:00+00:00	[thread overview]
Message-ID: <35E27BCC.CD70F24@rational.please_no_unsolicited_mail.com> (raw)
In-Reply-To: 35E16F1D.2E41DD75@tech.swh.lv

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




  parent reply	other threads:[~1998-08-25  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <35E16F1D.2E41DD75@tech.swh.lv>
1998-08-24  0:00 ` Dinamic type checking Samuel Tardieu
1998-08-25  0:00 ` Gene Ouye [this message]
1998-08-27  0:00 ` Matthew Heaney
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox