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 autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,68b702bd2264cada X-Google-Attributes: gid103376,public From: Tucker Taft Subject: Re: Dynamic redispatching Date: 2000/01/20 Message-ID: <38875677.950A6FCA@averstar.com>#1/1 X-Deja-AN: 575369035 Content-Transfer-Encoding: 7bit References: <862gtg$meu$1@nnrp1.deja.com> <864ir0$5up$1@nnrp1.deja.com> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@inmet2.burl.averstar.com X-Trace: inmet2.burl.averstar.com 948393592 3577 141.199.8.164 (20 Jan 2000 18:39:52 GMT) Organization: AverStar (formerly Intermetrics) Burlington, MA USA Mime-Version: 1.0 NNTP-Posting-Date: 20 Jan 2000 18:39:52 GMT Newsgroups: comp.lang.ada Date: 2000-01-20T18:39:52+00:00 List-Id: Ted Dennison wrote: > > In article , > Ray Blaak wrote: > > Perhaps this would work: > > > > Item.all := P1.Dispatch; > > Yes, that would work for the example I gave. However, it won't for this > example (which is closer to what I'm trying to do): > > 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; > > type Dispatcher_Routine is access function return Class; > Default_Dispatcher : Dispatcher_Routine := Main_Dispatcher'access; > end P1; > > with P1; > with P2; > > procedure Test is > > Item : P1.Handle := new P2.Instance; > begin > Item.all := P1.Default_Dispatcher.all; > end Test; > > IOW: I want to be able to supply function pointers that dispatch. Making > the type an "access function return Instance" doesn't work, because > P2.Dispatch'access isn't compatable with that type (P1.Instance and > P2.Instance are different types). So I tried making it classwide. That > works, except that the classwide function for some reason can't > redispatch to the appropriate primitive function. > > Is this just a limitation of functions in Ada? Well, that's a bit of a funny way to put it. Ada is the only OO language that does dispatching on result, by using context. This is analogous to the fact that Ada is one of the few languages that does overloading on result type. Dispatching on result is sort of like run-time overload resolution. However, for it to work, there must be *some* object with a tag around for the compiler to use to control the dispatching. Normally this would be the object that is the target of an assignment, or another parameter in a subprogram call with multiple controlling operands. You are trying to make this dispatching-on-result happen in a "return" statement, but at that point, there is no context from which to get the tag to control the dispatching. > ... Would changing everything > to procedures with "out" parameters solve the problem? You don't have to change "everything," but you will probably have to change the Main_Dispatcher into a procedure, and change the Dispatcher_Routine into an access-to-procedure. You can leave the individual Dispatcher's as functions that dispatch on result. However, returning composite objects as results is generally less efficient than using OUT parameters (because OUT parameters can use pass-by-reference). So unless there is some compelling reason for these to be functions (such as they have discriminants whose value is not known to the caller, or they will often be combined with other function calls in the middle of a more complex expression), you might want to make them all procedures. > > -- > T.E.D. > > http://www.telepath.com/~dennison/Ted/TED.html > > Sent via Deja.com http://www.deja.com/ > Before you buy. -- -Tucker Taft stt@averstar.com http://www.averstar.com/~stt/ Technical Director, Distributed IT Solutions (www.averstar.com/tools) AverStar (formerly Intermetrics, Inc.) Burlington, MA USA