[GAP] Ada 2005 object.method doesn't always dispatch

Cyrille Comar comar at adacore.com
Thu Feb 16 10:18:10 CET 2006


Martin Carlisle wrote:

> procedure Operation1 (This : access Parent) is
> begin
>       Put_Line("P:Operation1");
>    this.operation2;
> end Operation1;
>  
> I naively believed that when I did:
>  
> O2 : test_dispatching.classwide_ptr;
> begin
> O2 := new Test_Dispatching.Kid2;
> O2.Operation1;
>  
> that O2.Operation1 would dispatch to Operation1 from the Parent class (as
> Kid2 does not override it), and then inside operation1 that
> "this.operation2" would dispatch and do Operation2 from Kid2.  Instead,
> this.operation2 is NOT dispatching, and Operation2 from Grand_Parent gets
> called.
>  
> To get dispatching, I have to do:
> classwide_ptr(this).operation2
>  
> I suspect you may have students with Java or C# experience who will also
> stumble on this point, so I thought I would bring it to your attention.

Interesting!

if we go one step further, and think of proper resolution to this issue, 
I see 2 possibilities:

  1. operation1 is not really a Parent method  but rather a general 
utility for the whole class hierarchy. In which case you define it as

procedure Operation1 (This : access grand_parent'class) is
begin
     Put_Line("P:Operation1");
     this.operation2;
end Operation1;

and you get the dispatching you were looking for. Or Operation1 is a 
real method but you want to write it in a way that if it is not 
overriden it will go to the appropriate operation, in which case you 
write something like:

procedure Operation1 (This : access Parent) is
begin
     Put_Line("P:Operation1");
     Classwide_Ptr (this).operation2;
end Operation1;



More information about the GAP mailing list