comp.lang.ada
 help / color / mirror / Atom feed
* Re: Dinamic type checking
       [not found] <35E16F1D.2E41DD75@tech.swh.lv>
@ 1998-08-24  0:00 ` Samuel Tardieu
  1998-08-25  0:00 ` Gene Ouye
  1998-08-27  0:00 ` Matthew Heaney
  2 siblings, 0 replies; 3+ messages in thread
From: Samuel Tardieu @ 1998-08-24  0:00 UTC (permalink / raw)
  To: maxim_senin

>>>>> "Maxim" == Maxim Senin <maks@tech.swh.lv> writes:

Maxim> Can I do something like Java's "instanceof" operator does,
Maxim> i.e. can I check the object to be of type (or descendent of
Maxim> type)?

If A is an object of a tagged type and B is a tagged type, you can do:

    if A in B'Class then     --  Test that the type of A is of type B or 
                             --  of a type that inherits (directly or
                             --  indirectly) from type B

    if A in B then           --  Test that A is of type B

The second one works even for non-tagged types.

  Sam
-- 
Samuel Tardieu -- sam@ada.eu.org




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Dinamic type checking
       [not found] <35E16F1D.2E41DD75@tech.swh.lv>
  1998-08-24  0:00 ` Dinamic type checking Samuel Tardieu
@ 1998-08-25  0:00 ` Gene Ouye
  1998-08-27  0:00 ` Matthew Heaney
  2 siblings, 0 replies; 3+ messages in thread
From: Gene Ouye @ 1998-08-25  0:00 UTC (permalink / raw)


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




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Dinamic type checking
       [not found] <35E16F1D.2E41DD75@tech.swh.lv>
  1998-08-24  0:00 ` Dinamic type checking Samuel Tardieu
  1998-08-25  0:00 ` Gene Ouye
@ 1998-08-27  0:00 ` Matthew Heaney
  2 siblings, 0 replies; 3+ messages in thread
From: Matthew Heaney @ 1998-08-27  0:00 UTC (permalink / raw)


Maxim Senin <maks@tech.swh.lv> writes:

>  in Java:                               in Ada:
>  class A {}                             type A is tagged private;
>  class B extends A {}                   type B is new A with private;
>  ...                                    ...
>  B b;                                   objectOfTypeB : B;
>  ...                                    ...
>  if (b instanceof A) {                  if (objectOfTypeB'Tag = A'Tag) then
>      ...                                  -- this will not work 'cos "=" produces exact match
>  }                                      end if;

Use a membership test:

if objectOfTypeB in A'Class then ...;




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1998-08-27  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <35E16F1D.2E41DD75@tech.swh.lv>
1998-08-24  0:00 ` Dinamic type checking Samuel Tardieu
1998-08-25  0:00 ` Gene Ouye
1998-08-27  0:00 ` Matthew Heaney

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