comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: run-time type identification
Date: 1998/09/03
Date: 1998-09-03T00:00:00+00:00	[thread overview]
Message-ID: <m3zpch4qn2.fsf@mheaney.ni.net> (raw)
In-Reply-To: 6sm537$agc$1@emu.cs.rmit.edu.au

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:












  reply	other threads:[~1998-09-03  0:00 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-09-03  0:00 run-time type identification Bob Fletcher
1998-09-03  0:00 ` Samuel Tardieu
1998-09-03  0:00   ` Bob Fletcher
1998-09-03  0:00     ` Fergus Henderson
1998-09-03  0:00     ` Matthew Heaney
1998-09-06  0:00       ` Samuel Tardieu
1998-09-03  0:00 ` Tucker Taft
1998-09-03  0:00 ` Matthew Heaney
1998-09-03  0:00   ` Bob Fletcher
1998-09-03  0:00     ` Matthew Heaney
1998-09-03  0:00 ` Steve Sabljak
1998-09-03  0:00   ` Matthew Heaney [this message]
1998-09-03  0:00   ` Steve Sabljak
replies disabled

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