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

Martin Carlisle carlislem at acm.org
Wed Feb 15 21:37:44 CET 2006


I ran across the following issue, which caught me by surprise (though many
of you more Ada savvy types may find this immediately obvious):
 
I had the following declarations:
 
type Grand_Parent is abstract tagged record
X : Integer;
end record;
type classwide_ptr is access all grand_parent'class;
procedure Operation1(This : access Grand_Parent) is abstract;
procedure Operation2(This : access Grand_Parent);
 
type Parent is new Grand_Parent with null record;
procedure Operation1(This : access Parent);
type Kid1 is new Parent with null record;
procedure Operation1(This : access Kid1);
type Kid2 is new Parent with null record;
procedure Operation2(This : access Kid2);
 
And here was my body for Operation1 in the class "Parent"
 
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.
 
------------------------------------------------------------
//SIGNED//
Dr. Martin C. Carlisle
Associate Professor of Computer Science
United States Air Force Academy
Visiting Associate Professor
Auburn University Dept of CS and Software Engineering
307C Dunstan Hall, (334) 844-1432
http://www.martincarlisle.com
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/gap/attachments/20060215/22e5e29b/attachment.html


More information about the GAP mailing list