From: Kevin Ingalls <kevin.l.ingalls@boeing.com>
Subject: Dispatching In Generics
Date: 1997/06/05
Date: 1997-06-05T00:00:00+00:00 [thread overview]
Message-ID: <3396EB36.3D14@boeing.com> (raw)
First, I can't find the Ada FAQ, so perhaps the answer to my question is
there. If you know where it is I would appreciate the address.
Second, here is my coding question: How do I code a generic so that it
can take the base type of a hierarchy and perform dispatching on objects
of that type?
Specifically, given:
package A with an abstract tagged type X
a primitive operation "operation" on type X
child package A.B with a derived type Y
a primitive operation "operation" on type Y
How do I write a generic that can handle objects of X'class, including
dispatching operations correctly?
Here is the skeleton of a first try:
----------------------------
with ada.finalization;
package A is
type X is abstract tagged private;
procedure operation (anX : X);
private
type X is abstract new ada.finalization.controlled with...
end A;
package A.B is
type Y is new X with private;
procedure operation (aY : Y);
private
type Y is new X with record...
end A.B;
generic
type T is abstract tagged private;
with procedure op (aT : T);
package gPackage is
procedure P;
end gPackage;
package body gPackage is
procedure P is
begin
op (object_of_type_T); -- here is the syntax error
end P;
end gPackage;
with A, gPackage;
package instance is new gPackage(A.X, A.operation);
----------------------------
The syntax error occurs while compiling the generic on the marked line.
I understand that I have no subprograms with class-wide parameters, and
so "op" cannot do dispatching (which is what the error message tells
me). The question is, what is the correct way to accomplish this?
As a kludge, I added a class-wide operation to package A for type X:
----
procedure operation_class_wide (aClassWideX : X'class);
----
procedure operation_class_wide (aClassWideX : X'class) is
begin
operation(aClassWideX);
end operation_class_wide;
And changed the instantiation to:
package instance is new gPackage (A.X, A.operation_class_wide);
But this is hardly a clean solution. So, the question is how can I write
a generic package to handle a class hierarchy, including
dispatching-like behavior, cleanly?
--
Kevin Ingalls, (206)773-9404, kevin.l.ingalls@boeing.com
next reply other threads:[~1997-06-05 0:00 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-06-05 0:00 Kevin Ingalls [this message]
1997-06-07 0:00 ` Dispatching In Generics Tom Moran
1997-06-09 0:00 ` Robert A Duff
1997-06-09 0:00 ` John English
-- strict thread matches above, loose matches on Subject: below --
1997-09-11 0:00 Dispatching in Generics Kevin Ingalls
1997-09-12 0:00 ` Robert A Duff
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox