From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: * X-Spam-Status: No, score=1.3 required=5.0 tests=BAYES_00,INVALID_MSGID, MSGID_RANDY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,68b702bd2264cada,start X-Google-Attributes: gid103376,public From: Ted Dennison Subject: Dynamic redispatching Date: 2000/01/18 Message-ID: <862gtg$meu$1@nnrp1.deja.com>#1/1 X-Deja-AN: 574316881 X-Http-Proxy: 1.0 x36.deja.com:80 (Squid/1.1.22) for client 204.48.27.130 Organization: Deja.com - Before you buy. X-Article-Creation-Date: Tue Jan 18 20:03:07 2000 GMT X-MyDeja-Info: XMYDJUIDtedennison Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.6 [en] (WinNT; I) Date: 2000-01-18T00:00:00+00:00 List-Id: 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.