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

Tullio Vardanega tullio.vardanega at math.unipd.it
Thu Feb 16 11:42:28 CET 2006


Martin, Cyrille and All,

I have been following with interest this thread of discussion
and hacked together the various fragments of code proposed
by Martin and Cyrille into a GAP-compilable project.

If you have the patience of unzipping the project, browse
through the code (which is basically Martin's and Cyrille's) and
compile it, you will note an interesting malfunction in the compiler
(in main.adb), which seems to dislike the object.method syntax
for the example.

-- Tullio
> 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;
>
> _______________________________________________
> GAP mailing list
> GAP at gnat.info
> /no-more-mailman.html
> To unsubscribe from this list, please contact the GAP GNAT Tracker 
> administrator
> within your organization.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Carlisle.zip
Type: application/x-zip-compressed
Size: 2066 bytes
Desc: not available
Url : /pipermail/gap/attachments/20060216/e48814f0/Carlisle.zip


More information about the GAP mailing list