comp.lang.ada
 help / color / mirror / Atom feed
* Re: Ada95 and Dynamic Type Identification
       [not found] <milodD2Ko8z.98w@netcom.com>
@ 1995-01-18  8:48 ` Stephane Barbey
  1995-01-18 12:39 ` Robert A Duff
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 4+ messages in thread
From: Stephane Barbey @ 1995-01-18  8:48 UTC (permalink / raw)


In article <milodD2Ko8z.98w@netcom.com>, milod@netcom.com (John DiCamillo) writes:
: 
: Does Ada95 have some sort of dynamic type identification
: equivalent to C++ RTTI or Eiffel's assignment-attempt?

yes.

: That is, given a procedure
: 
:    procedure Process_Alerts(A: Alert'Class) ...
: 
: Is there any way to find out precisely what type of
: object A is?  Or are we limited to dynamic dispatch?
: 
: e.g.:
: 
:       foo: FooAlert;         -- special alert
:    begin
:       foo ?= A;              -- is A a FooAlert?
:       if (foo'Valid) then    -- A IS a FooAlert!
:          FooHandler( foo );  -- do something that only
:                              -- applies to FooAlerts
: 

        type FooAlert is tagged ...
        type BarAlert is new FooAlert with ...  
        procedure BarHandler (Bar: BarAlert) 
          -- BarHandler only applies to Bars.
  
  
        A: FooAlert'Class := ...; -- could be a Foo- or a BarAlert
        if A in BarAlert then    -- the membership test
           Primitive (BarAlert(A))

: Anyway, if dynamic typing was left out of Ada95, would 
: that be considered a mistake.  Every other statically
: typed OOPL I know of has eventually provided some form
: of dynamic typing.  Why not Ada?  Is there some subtlety
: about Ada's type system that makes dynamic typing un-
: necessary or irrelevant?

No. There is same kind of dynamic typing in Ada, see "class-wide" in the
index of Barnes book.

-Stephane

--
Stephane Barbey				"It's not easy getting real wings"	
INJ-335, barbey@di.epfl.ch



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

* Re: Ada95 and Dynamic Type Identification
       [not found] <milodD2Ko8z.98w@netcom.com>
  1995-01-18  8:48 ` Ada95 and Dynamic Type Identification Stephane Barbey
@ 1995-01-18 12:39 ` Robert A Duff
  1995-01-18 17:33 ` David Moore
  1995-01-18 19:12 ` R. William Beckwith
  3 siblings, 0 replies; 4+ messages in thread
From: Robert A Duff @ 1995-01-18 12:39 UTC (permalink / raw)


In article <milodD2Ko8z.98w@netcom.com>,
John DiCamillo <milod@netcom.com> wrote:
>
>Does Ada95 have some sort of dynamic type identification
>equivalent to C++ RTTI or Eiffel's assignment-attempt?
>That is, given a procedure
>
>   procedure Process_Alerts(A: Alert'Class) ...
>
>Is there any way to find out precisely what type of
>object A is?

You can say:

    if A in This_Type'Class then
        This_Type'Class(A).Some_Field := ...;
          -- Some_Field is declared in This_Type.
    elsif A in That_Type'Class then
        ...
    else
        ...
    end if;

where This_Type and That_Type are descendants of Alert.  Or, leave off
the 'Class if you want to check whether A is a member of the specific
types, and not any further descendants of them.

A type conversion from A to This_Type'Class (or This_Type) will
automatically do a run-time check, to make sure A in This_Type'Class (or
This_Type, resp.).  The type conversion in the above example can't fail,
and a decent compiler will not generate any code for it.

You can also say "if A'Tag = This_Type'Tag then ...",
which is the same as "if A in This_Type then ...".
However, 'Tag is more useful to check whether two
objects came from the same type, as in:

    if Set1'Tag = Set2'Tag then
        declare
            Set3: Set'Class := Union(Set1, Set2);
            ...

>I couldn't find any references to this ability in Barnes'
>"Introducing Ada 9X" or the Rationale, but I wasn't sure
>what topic to look for either.

If it's not there (or deeply hidden), it's probably because the *usual*
way of checking the tag of an object is the implicit way that happens
during dispatch.  That's true of Eiffel and C++, too.  I suspect all of
the above methods are *somewhere* in the Rat (they ought to be),
although they are likely not in the Intro document.

- Bob



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

* Re: Ada95 and Dynamic Type Identification
       [not found] <milodD2Ko8z.98w@netcom.com>
  1995-01-18  8:48 ` Ada95 and Dynamic Type Identification Stephane Barbey
  1995-01-18 12:39 ` Robert A Duff
@ 1995-01-18 17:33 ` David Moore
  1995-01-18 19:12 ` R. William Beckwith
  3 siblings, 0 replies; 4+ messages in thread
From: David Moore @ 1995-01-18 17:33 UTC (permalink / raw)


milod@netcom.com (John DiCamillo) writes:


>Does Ada95 have some sort of dynamic type identification
>equivalent to C++ RTTI or Eiffel's assignment-attempt?
>That is, given a procedure

>   procedure Process_Alerts(A: Alert'Class) ...

>Is there any way to find out precisely what type of
>object A is?  Or are we limited to dynamic dispatch?

There is an attribute "tag". So if we want to determine if
A is of a particular type (Dozing, say) we can either write:

if a'tag=dozing'tag then

or we can write:

if a in dozing then

There is also a predefined package that will return textual names
of types. 

package ada.tags is
   type tag is private;
   function expanded_name(t:tag) return string;
   function external_tag(t:tag) return string;
   function internal_tag(external:string) return tag;
private 
...
end ada.tags;

Note we can also say

if a in dozing'class

which will be true if a is in any class derived from the tagged type.




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

* Re: Ada95 and Dynamic Type Identification
       [not found] <milodD2Ko8z.98w@netcom.com>
                   ` (2 preceding siblings ...)
  1995-01-18 17:33 ` David Moore
@ 1995-01-18 19:12 ` R. William Beckwith
  3 siblings, 0 replies; 4+ messages in thread
From: R. William Beckwith @ 1995-01-18 19:12 UTC (permalink / raw)


John DiCamillo (milod@netcom.com) wrote:

: Does Ada95 have some sort of dynamic type identification
: equivalent to C++ RTTI or Eiffel's assignment-attempt?
: That is, given a procedure

:    procedure Process_Alerts(A: Alert'Class) ...

: Is there any way to find out precisely what type of
: object A is?  Or are we limited to dynamic dispatch?

: e.g.:

:       foo: FooAlert;         -- special alert
:    begin
:       foo ?= A;              -- is A a FooAlert?
:       if (foo'Valid) then    -- A IS a FooAlert!
:          FooHandler( foo );  -- do something that only
:                              -- applies to FooAlerts

4.5.2 Relational Operators and Membership Tests

    if A in FooAlert'CLASS then       -- is A a FooAlert?
       FooHandler(FooAlert'CLASS(A)); -- do something that only
    end if;                           -- applies to FooAlerts

The FooAlert'CLASS(A) type conversion is thus guaranteed
to work.

: Anyway, if dynamic typing was left out of Ada95, would 
: that be considered a mistake. Every other statically
: typed OOPL I know of has eventually provided some form
: of dynamic typing.  Why not Ada?  Is there some subtlety
: about Ada's type system that makes dynamic typing un-
: necessary or irrelevant?

Yes, leaving dynamic typing would have been a mistake.
I'm sure glad Ada95 has such a nice dynamic typing system.

... Bill



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

end of thread, other threads:[~1995-01-18 19:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <milodD2Ko8z.98w@netcom.com>
1995-01-18  8:48 ` Ada95 and Dynamic Type Identification Stephane Barbey
1995-01-18 12:39 ` Robert A Duff
1995-01-18 17:33 ` David Moore
1995-01-18 19:12 ` R. William Beckwith

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