comp.lang.ada
 help / color / mirror / Atom feed
* access types and dispatching subprograms
@ 1996-06-23  0:00 kbrun
  1996-06-23  0:00 ` Robert Dewar
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: kbrun @ 1996-06-23  0:00 UTC (permalink / raw)



I have some trouble with Ada95 dispatching subprograms and access types.
AFAIK, a proc. call should dispatch if all corresponding actual parameters
are of (*or pointing to*) type T'Class and if the formal parameters are
of (*or pointing to*) type T. Still, the following code sample doesn't
work.

--

package T is

   type T is tagged null record;
   type T_p is access all T;
   type TC_p is access all T'class;

   type T1 is new T with null record;
   type T1_p is access all T1;

   procedure disp(p: T_p);
   procedure disp(p: T1_p);

   procedure do_dispatch;
end T;

package body T is

   procedure disp(p: T_p) is
   begin
     Put("T_p");
   end disp;

   procedure disp(p: T1_p) is
   begin
     Put("T1_p");
   end disp;

   procedure do_dispatch is
      classp: TC_p := new T1;
   begin
      disp(classp);	-- this call won't dispatch, instead it causes
                        -- a compiler error. what's wrong?
   end do_dispatch;
end x;

--

Could someone please enlighten me on this subject?

-- Klaus A. Brunner   Austria, Europe
-- mailto:kbrun@ibm.net





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

* Re: access types and dispatching subprograms
  1996-06-23  0:00 access types and dispatching subprograms kbrun
  1996-06-23  0:00 ` Robert Dewar
@ 1996-06-23  0:00 ` Robert A Duff
  1996-06-25  0:00 ` Samuel Tardieu
  2 siblings, 0 replies; 4+ messages in thread
From: Robert A Duff @ 1996-06-23  0:00 UTC (permalink / raw)



In article <4qjqre$44lo@news-s01.ny.us.ibm.net>,  <kbrun@ibm.net> wrote:
>   procedure disp(p: T_p);

You want "p: access T" here.  Access parameters, which are of an
anonymous access type, allow dispatching.  A named access type, such as
T_p, does not.

>   procedure disp(p: T1_p);

And "p: access T1" here.

>   procedure do_dispatch is
>      classp: TC_p := new T1;
>   begin
>      disp(classp);	-- this call won't dispatch, instead it causes
>                        -- a compiler error. what's wrong?

With the above changes, this will dispatch.

The reason you get a compile-time error is because we tried to set up
the rules so that if you *think* you are calling a dispatching
operation, but you are not, you will get a compile-time error if you
call it in a dispatching way.  As opposed to doing the unexpected thing
at run time.  This case is one example of that.

- Bob




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

* Re: access types and dispatching subprograms
  1996-06-23  0:00 access types and dispatching subprograms kbrun
@ 1996-06-23  0:00 ` Robert Dewar
  1996-06-23  0:00 ` Robert A Duff
  1996-06-25  0:00 ` Samuel Tardieu
  2 siblings, 0 replies; 4+ messages in thread
From: Robert Dewar @ 1996-06-23  0:00 UTC (permalink / raw)


Klaus asked about dispatching.

First, I am suspicious of the sources you sent, since the package body
of t ends with

end x;

which obviously could not compile

second, you have undefined references to Put

After these fixes, I get "invalid parameter list in call", which certainly
seems a correct diagnostic to me, since you have no procedure matching the
parameters that you gave.





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

* Re: access types and dispatching subprograms
  1996-06-23  0:00 access types and dispatching subprograms kbrun
  1996-06-23  0:00 ` Robert Dewar
  1996-06-23  0:00 ` Robert A Duff
@ 1996-06-25  0:00 ` Samuel Tardieu
  2 siblings, 0 replies; 4+ messages in thread
From: Samuel Tardieu @ 1996-06-25  0:00 UTC (permalink / raw)
  To: kbrun


>>>>> "Klaus" == kbrun  <kbrun@ibm.net> writes:

Klaus> I have some trouble with Ada95 dispatching subprograms and
Klaus> access types. AFAIK, a proc. call should dispatch if all
Klaus> corresponding actual parameters are of (*or pointing to*) type
Klaus> T'Class and if the formal parameters are of (*or pointing to*)
Klaus> type T. Still, the following code sample doesn't work.

It seems you haven't well understood how dispatching works. The
"compiler error" (in fact "invalid parameter list in call") seems
correct, because there is no procedure "disp" whose formals are of
type TC_p.

Dispatching works on subprograms whose first parameter is of a tagged
type (or access to a tagged type) (in fact all the parameters of the
same hierarchy of tagged types).

What you probably want to do is:

package T is

   type T is tagged null record;
   procedure Disp (P : access T);

   type T1 is new T with null record;
   procedure Disp (P : access T1);

   type TC_p is access all T'Class;

   procedure Do_Dispatch;

end T;

with Ada.Text_IO; use Ada.Text_IO;
package body T is

   procedure Disp (P : access T) is
   begin
      Put_Line ("access T");
   end Disp;

   procedure Disp (P : access T1) is
   begin
      Put_Line ("access T1");
   end Disp;

   procedure Do_Dispatch is
      ClassP : constant TC_p := new T1;
   begin
      Disp (ClassP);
   end Do_Dispatch;

end T;

with T; 
procedure Test is
begin
   T.Do_Dispatch;
end Test;

Execution gives:

% ./test
access T1

which is what you want.

   Sam

PS/ Note the use of access parameters (and not parameters of an access type)
-- 
  Samuel Tardieu -- sam@ada.eu.org




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

end of thread, other threads:[~1996-06-25  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-06-23  0:00 access types and dispatching subprograms kbrun
1996-06-23  0:00 ` Robert Dewar
1996-06-23  0:00 ` Robert A Duff
1996-06-25  0:00 ` Samuel Tardieu

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