comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: How to?: Re-dispatching within an inherited method
Date: 1999/08/17
Date: 1999-08-17T00:00:00+00:00	[thread overview]
Message-ID: <7pcar9$aeg@hobbes.crc.com> (raw)
In-Reply-To: 37b9a823@news1.us.ibm.net


Matthew Heaney <matthew_heaney@acm.org> wrote in message
news:37b9a823@news1.us.ibm.net...
> In article <7onk8f$2qg@hobbes.crc.com> , "David C. Hoos, Sr."
> <david.c.hoos.sr@ada95.com> wrote:
>
> > Yes.  You need to type convert the actual parameter in the call to
> > Widget'Class to make the call a dispatching call.  See RM 95 3.9.2 (1,
5).
> >
> > As in the example:
> >
> > with Ada.Text_IO;
> > package body A is
> >
> >    procedure Run_Widget (The_Widget : Widget'Class) is
> >    begin
> >       Ada.Text_IO.Put_Line
> >         ("in Run_Widget (The_Widget : Widget): Size =" &
> >          Positive'Image (Widget_Size (Widget'Class (The_Widget))));
>
> Did you really mean to declare the formal parameter The_Widget in
operation
> Run_Widget as having type Widget'Class?  If so, then there's no need for
> your explicit conversion of The_Widget to type Widget'Class, because
that's
> already its type.
>
> The other problem with this solution is that Run_Widget is no longer
> primitive, because it takes a class-wide parameter.  The operation is NOT
> inherited during derivation.
>
> I suspect that what you really meant was
>
>   procedure Run_Widget (The_Widget : Widget) is
>   begin
>     ... Widget_Size (Widget'Class (The_Widget)) ...
>   end;
>
>
Well... I really did mean to make Run_Widget class-wide.  This
may not have beeen consistent with the posting's subject line,
however.

Without completely redesigning the type hierarchy (I would
probably have made Widget abstract, and gone from there),
I was trying to get at the dispatching issue.

And... yes, having made Run_Widget class-wide, the type
conversion is unneccessary.

Without a class-wide Run_Widget, however, the call to
Run_Widget with an actual parameter of type BigWidget is
illegal -- a type conversion is required there.

> >    end Run_Widget;
> >
> >    function Widget_Size (The_Widget : Widget) return Positive is
> >    begin
> >       return 1;
> >    end Widget_Size;
> >
> > end A;
> >
> >
> > package A is
> >    type Widget is tagged null record;
> >    procedure Run_Widget (The_Widget : Widget'Class);
>
> I think you want this:
>
>   procedure Run_Widget (The_Widget : Widget);
>
>
> >    function Widget_Size (The_Widget : Widget) return Positive;
> > end A;
> >
> > package body B is
> >    function Widget_Size (The_Widget : BigWidget) return Positive is
> >    begin
> >       return 10;
> >    end Widget_Size;
> > end B;
> >
> > with A;
> > package B is
> >    type BigWidget is new A.Widget with null record;
> >    function Widget_Size (The_Widget : BigWidget) return Positive;
> > end B;
> >
> > with A;
> > with B;
> > procedure Glover is
> >
> >    The_Widget : A.Widget;
> >    The_BigWidget : B.BigWidget;
> >
> > begin
> >
> >    A.Run_Widget (The_Widget);
> >    A.Run_Widget (The_BigWidget);
>
> With the change, you can say:
>
>   B.Run_Widget (The_BigWidget);
>
> and all is well.
>
> > end Glover;
>
> --
> Matt
>
> It is impossible to feel great confidence in a negative theory which has
> always rested its main support on the weak points of its opponent.
>
> Joseph Needham, "A Mechanistic Criticism of Vitalism"






  reply	other threads:[~1999-08-17  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-08-09  0:00 How to?: Re-dispatching within an inherited method Anthony E. Glover
1999-08-09  0:00 ` David C. Hoos, Sr.
1999-08-17  0:00   ` Matthew Heaney
1999-08-17  0:00     ` David C. Hoos, Sr. [this message]
1999-08-18  0:00       ` Matthew Heaney
1999-08-18  0:00         ` Anthony E. Glover
1999-08-19  0:00           ` Matthew Heaney
1999-08-21  0:00             ` Anthony E. Glover
replies disabled

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