comp.lang.ada
 help / color / mirror / Atom feed
From: Tucker Taft <stt@averstar.com>
Subject: Re: Dynamic redispatching
Date: 2000/01/20
Date: 2000-01-20T18:39:52+00:00	[thread overview]
Message-ID: <38875677.950A6FCA@averstar.com> (raw)
In-Reply-To: 864ir0$5up$1@nnrp1.deja.com

Ted Dennison wrote:
> 
> In article <m3hfgazg42.fsf@baka.infomatch.bc.ca>,
>   Ray Blaak <blaak@infomatch.com> 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




  reply	other threads:[~2000-01-20  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2000-01-20  0:00       ` Ted Dennison
replies disabled

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