comp.lang.ada
 help / color / mirror / Atom feed
* Run Time Dispatch Question
@ 2000-02-25  0:00 Michael Garrett
  2000-02-25  0:00 ` John English
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Garrett @ 2000-02-25  0:00 UTC (permalink / raw)



I am back to my study of Ada...

I am having a problem understanding run time dispatching. ( Basic question I
know, but I'm having a brain cramp. )


I have declared a basic type using a class wide access variable link to the
next object.
When I create a simple linked list and a class wide access variable to point
into the list,
I get a compile time error when calling procedure p( .... ), a dispatching
procedure.
( which works for compile time dispatching ).


What is my problem???


with ada.text_io;
use ada.text_io;

procedure main7 is
-------------------------------------------------------
    type t;
    type pt is access all t'class;

    type t is tagged record
        next : pt := null;
    end record;
 -------------------------------------------------------
    type t1 is new t with record
        x : integer;
    end record;

    procedure p( this : t1 ) is
    begin
        put_line(" in p(t1)");
    end p;
-------------------------------------------------------
    type t2 is new t1 with record
        y : integer;
    end record;

    procedure p( this : t2 ) is
    begin
        put_line("in p(t2)");
    end p;
-------------------------------------------------------
begin
    -- static dispatching, compile time
    put_line("Compile Time Dispatching");
    declare
        tt1 : t1;
        tt2 : t2;
    begin
        tt1.x := 10;
        tt2.x := 5;
        tt2.y := 40;
        p(tt1);
        p(tt2);
    end;
    -- end compile time dispatching

    put_line("Run Time Dispatching");
    declare
        ptrt : pt;
        it : pt;        -- classwide access type
    begin
        ptrt := new t1;
        ptrt.next := new t2;
        it := ptrt.next; -- it references a t2 object, second in list.
        p(it.all);    -- PROBLEM
    end;

end main7;




--
Thank You!!
Michael C. Garrett







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

* Re: Run Time Dispatch Question
  2000-02-25  0:00 Run Time Dispatch Question Michael Garrett
@ 2000-02-25  0:00 ` John English
  2000-02-26  0:00   ` Michael Garrett
  0 siblings, 1 reply; 3+ messages in thread
From: John English @ 2000-02-25  0:00 UTC (permalink / raw)


Michael Garrett wrote:
> I am having a problem understanding run time dispatching. ( Basic question I
> know, but I'm having a brain cramp. )
> [...snip...]

You need to put your tagged type t and primitive p in a package, not
a procedure. Otherwise, the procedure p(...) defined for each type
derived from t is just an overload of the name p, not a primitive of
the type. Dispatching only happens for primitives. To quote RM95
3.2.3:

    The primitive subprograms of a specific type are defined as
    follows: 
    a) The predefined operators of the type (see 4.5); 
    b) For a derived type, the inherited (see 3.4) user-defined
       subprograms; 
    c) For an enumeration type, the enumeration literals (which are
       considered parameterless functions -- see 3.5.1); 
    d) For a specific type declared immediately within a
       package_specification, any subprograms (in addition to the
       enumeration literals) that are explicitly declared immediately
       within the same package_specification and that operate on
       the type; 
    e) Any subprograms not covered above that are explicitly declared
       immediately within the same declarative region as the
       type and that override (see 8.3) other implicitly declared
       primitive subprograms of the type. 

So, if t and p were declared in a package then p would be a primitive
(by (d)), and overloading p for t1 and t2 would make them primitives
too (by (e)).

-----------------------------------------------------------------
 John English              | mailto:je@brighton.ac.uk
 Senior Lecturer           | http://www.it.bton.ac.uk/staff/je
 Dept. of Computing        | ** NON-PROFIT CD FOR CS STUDENTS **
 University of Brighton    |    -- see http://burks.bton.ac.uk
-----------------------------------------------------------------




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

* Re: Run Time Dispatch Question
  2000-02-25  0:00 ` John English
@ 2000-02-26  0:00   ` Michael Garrett
  0 siblings, 0 replies; 3+ messages in thread
From: Michael Garrett @ 2000-02-26  0:00 UTC (permalink / raw)



John English <je@bton.ac.uk> wrote in message
news:38B6837E.7208F8C8@bton.ac.uk...
> Michael Garrett wrote:
> > I am having a problem understanding run time dispatching. ( Basic
question I
> > know, but I'm having a brain cramp. )
> > [...snip...]
>
> You need to put your tagged type t and primitive p in a package, not
> a procedure. Otherwise, the procedure p(...) defined for each type
> derived from t is just an overload of the name p, not a primitive of
> the type. Dispatching only happens for primitives. To quote RM95
> 3.2.3:
>
>

Thank You!!

This clears up most of my confusion.



" Build it, Then read the instructions".

Michael






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

end of thread, other threads:[~2000-02-26  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-02-25  0:00 Run Time Dispatch Question Michael Garrett
2000-02-25  0:00 ` John English
2000-02-26  0:00   ` Michael Garrett

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