comp.lang.ada
 help / color / mirror / Atom feed
* Dynamic redispatching
@ 2000-01-18  0:00 Ted Dennison
  2000-01-18  0:00 ` Ray Blaak
  0 siblings, 1 reply; 5+ messages in thread
From: Ted Dennison @ 2000-01-18  0:00 UTC (permalink / raw)


There's something about classwide programming I'm just not getting, and
I was hoping some of you experts here could help enlighten me.

I'm trying to write a classwide function that dynamicly redispatches
based on the tag of the function result. But it isn't dispatching on
either of the two Ada compilers I tried. Here's an example:

package P1 is
   type Instance is tagged null record;
   subtype Class is Instance'Class;
   type Handle is access all Class;

   function Dispatch return Instance;

   function Main_Dispatcher return Class;
end P1;

with Ada.Text_IO;

package body P1 is
   function Dispatch return Instance is
      Result : Instance;
   begin
      Ada.Text_IO.Put_Line ("P1 Dispatch");
      return Result;
   end Dispatch;

   function Main_Dispatcher return Class is
   begin
      return Dispatch;
   end Main_Dispatcher;
end P1;

But it appears that rather than dynamicly dispatching based on the
return type of Main_Dispatcher, it just always calls P1.Dispatch. IOW:

with P1;
package P2 is
   type Instance is new P1.Instance with null record;
   subtype Class is Instance'Class;
   type Handle is access all Class;

   function Dispatch return Instance;
end P2;

with Ada.Text_IO;
package body P2 is
   function Dispatch return Instance is
      Result : Instance;
   begin
      Ada.Text_IO.Put_Line ("P2 Dispatch");
      return Result;
   end Dispatch;
end P2;

with P1;
with P2;
procedure Test is
   Item : P1.Handle := new P2.Instance;
begin
   Item.all := P1.Main_Dispatcher;
end Test;

When I run Test, of course I want it to dispatch to P2.Dispatch and
return a value of type P2.Instance. Instead what happens is that it
dispatches to P1.Dispatch, prints "P1 Dispatch", then raises constraint
error because Item.all's tag doesn't match that of the return value.

I'd like to know exactly why it is doing this, and if there's any way to
get it to get the classwide function to properly redispatch.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com http://www.deja.com/
Before you buy.




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

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

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-01-18  0:00 Dynamic redispatching Ted Dennison
2000-01-18  0:00 ` Ray Blaak
2000-01-19  0:00   ` Ted Dennison
2000-01-20  0:00     ` Tucker Taft
2000-01-20  0:00       ` Ted Dennison

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