* class wide object in generic package
@ 2002-06-10 7:38 Thomas Maier-Komor
2002-06-10 12:28 ` Craig Carey
2002-06-10 13:00 ` Stephen Leake
0 siblings, 2 replies; 10+ messages in thread
From: Thomas Maier-Komor @ 2002-06-10 7:38 UTC (permalink / raw)
Hi everybody,
I am getting this error message when compiling the
code at the end of the message:
generic_pac.adb:7:26: class-wide argument not allowed here
generic_pac.adb:7:26: "eval" is not a primitive operation of "Object"
my question is:
first: is it possible to declare a procedure in the
generic part so that it will be a primitive operation of
Object?
second: why isn't a class-wide argument not allowed
in this place?
Thanks,
Tom
generic_pac.ads:
generic
type Object is abstract tagged private;
type Pointer is access all Object'class;
with procedure eval(o : in out Object);
package generic_pac is
type gen is record
p : Pointer;
end record;
procedure use_it(g : in out gen);
end generic_pac;
===========================================
generic_pac.adb:
with Ada.Text_IO; use Ada.Text_IO;
package body generic_pac is
procedure use_it(g : in out gen) is
begin
Put_Line("generic_pac.use_it");
eval(g.p.all);
end use_it;
end generic_pac;
--
________________________________________________________________________
Dipl.-Ing. Thomas Maier-Komor http://www.rcs.ei.tum.de
Institute for Real-Time Computer Systems (RCS) fon +49-89-289-23578
Technische Universitaet Muenchen, D-80290 Muenchen fax +49-89-289-23555
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: class wide object in generic package
@ 2002-06-10 11:07 Grein, Christoph
0 siblings, 0 replies; 10+ messages in thread
From: Grein, Christoph @ 2002-06-10 11:07 UTC (permalink / raw)
> first: is it possible to declare a procedure in the
> generic part so that it will be a primitive operation of
> Object?
> second: why isn't a class-wide argument not allowed
> in this place?
>
> generic_pac.ads:
> generic
> type Object is abstract tagged private;
> type Pointer is access all Object'class;
> with procedure eval(o : in out Object);
You can instantiate with any procedure that matches the profile, so the worst is
assumed and Eval is not a primitive operation.
>
> package generic_pac is
> type gen is record
> p : Pointer;
> end record;
>
> procedure use_it(g : in out gen);
>
> end generic_pac;
> ===========================================
> generic_pac.adb:
> with Ada.Text_IO; use Ada.Text_IO;
>
> package body generic_pac is
> procedure use_it(g : in out gen) is
> begin
> Put_Line("generic_pac.use_it");
> eval(g.p.all);
Since Pointer is classwide, this is a dispatching call, but Eval is not
primitive, so can't be used in a dispatching call.
> end use_it;
> end generic_pac;
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: class wide object in generic package
2002-06-10 7:38 class wide object in generic package Thomas Maier-Komor
@ 2002-06-10 12:28 ` Craig Carey
2002-06-10 13:14 ` Thomas Maier-Komor
2002-06-10 13:00 ` Stephen Leake
1 sibling, 1 reply; 10+ messages in thread
From: Craig Carey @ 2002-06-10 12:28 UTC (permalink / raw)
On Mon, 10 Jun 2002 09:38:15 +0200, Thomas Maier-Komor
<maierkom@rcs.ei.tum.de> wrote:
...
>I am getting this error message when compiling the
>code at the end of the message:
>
>generic_pac.adb:7:26: class-wide argument not allowed here
>generic_pac.adb:7:26: "eval" is not a primitive operation of "Object"
>
...
>second: why isn't a class-wide argument not allowed
>in this place?
>
That error message is one that GNAT 3.14p for NT produces, and it
identifies the 2nd "a" in the text "eval (g.p.all)", in the
generic_pac.adb file.
Putting in a type conversion to solve the Object vs Object'Class
mismatch, causes that error to not appear. Here is GNAT listing
output:
| gnatmake generic_pac.adb -gnatl -gnatwa -gnata -gnato -gnatq -gnatf
| -v -gnatU -gnatn -g -O0 -gnaty3abcefhikrlM79pts -gnatwa -bargs -E
| -p -we -static -largs -v -v -L.
|File: generic_pac.ads
| 1. generic
| 2. type Object is abstract tagged private;
| 3. type Pointer is access all Object'class;
| 4. with procedure eval (o : in out Object);
| 5.
| 6. package generic_pac is
| 7. type gen is record
| 8. p : Pointer;
| 9. end record;
| 10.
| 11. procedure use_it (g : in out gen);
| 12.
| 13. end generic_pac;
|
|File: generic_pac.adb
| 1. with Ada.Text_IO; use Ada.Text_IO;
| 2.
| 3. package body generic_pac is
| 4. procedure use_it (g : in out gen) is
| 5. begin
| 6. Put_Line ("generic_pac.use_it");
| 7. eval (Object (g.p.all));
| 8. end use_it;
| 9. end generic_pac;
|
Craig Carey / Ada mailing lists: http://www.ijs.co.nz/ada_95.htm
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: class wide object in generic package
2002-06-10 7:38 class wide object in generic package Thomas Maier-Komor
2002-06-10 12:28 ` Craig Carey
@ 2002-06-10 13:00 ` Stephen Leake
2002-06-10 13:25 ` Thomas Maier-Komor
2002-06-11 20:57 ` Ehud Lamm
1 sibling, 2 replies; 10+ messages in thread
From: Stephen Leake @ 2002-06-10 13:00 UTC (permalink / raw)
Thomas Maier-Komor <maierkom@rcs.ei.tum.de> writes:
> Hi everybody,
>
> I am getting this error message when compiling the
> code at the end of the message:
>
> generic_pac.adb:7:26: class-wide argument not allowed here
> generic_pac.adb:7:26: "eval" is not a primitive operation of "Object"
>
>
> my question is:
> first: is it possible to declare a procedure in the
> generic part so that it will be a primitive operation of
> Object?
No. All primitive operations are declared in the same declarative
region as the type.
> second: why isn't a class-wide argument not allowed in this place?
Because "eval" isn't a primitive operation of Object :). Using a
class-wide argument means you _want_ dispatching. Only primitive
operations can be dispatching.
You have a couple choices to make this work. If you don't actually
need dispatching, you can make eval take a class-wide argument:
with procedure eval(o : in out Object'class);
If you want dispatching, you'll have to use a generic formal package
parameter:
generic
package Object_Package is new Generic_Obj_Package (...);
package generic_pac ...
Then in Generic_Obj_Package, declare Object and Eval. Note that it has
to be generic!
Good luck,
--
-- Stephe
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: class wide object in generic package
2002-06-10 12:28 ` Craig Carey
@ 2002-06-10 13:14 ` Thomas Maier-Komor
0 siblings, 0 replies; 10+ messages in thread
From: Thomas Maier-Komor @ 2002-06-10 13:14 UTC (permalink / raw)
Thanks,
the error-message was a little bit confusing...
Thomas Maier-Komor
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: class wide object in generic package
2002-06-10 13:00 ` Stephen Leake
@ 2002-06-10 13:25 ` Thomas Maier-Komor
2002-06-11 20:57 ` Ehud Lamm
1 sibling, 0 replies; 10+ messages in thread
From: Thomas Maier-Komor @ 2002-06-10 13:25 UTC (permalink / raw)
>
> generic
> package Object_Package is new Generic_Obj_Package (...);
> package generic_pac ...
>
> Then in Generic_Obj_Package, declare Object and Eval. Note that it has
> to be generic!
>
well, I tried exactly this before I posted and I got the same
result...
Now I used a simple cast as Craig Carey suggested and
it works fine _with_ dispatching and without further files
involved.
But thanks anyway,
Thomas Maier-Komor
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: class wide object in generic package
2002-06-10 13:00 ` Stephen Leake
2002-06-10 13:25 ` Thomas Maier-Komor
@ 2002-06-11 20:57 ` Ehud Lamm
2002-06-12 17:33 ` Stephen Leake
1 sibling, 1 reply; 10+ messages in thread
From: Ehud Lamm @ 2002-06-11 20:57 UTC (permalink / raw)
"Stephen Leake" <stephen_leake@acm.org> wrote in message
news:wkelffffuy.fsf@acm.org...
> Thomas Maier-Komor <maierkom@rcs.ei.tum.de> writes:
>
>
> If you want dispatching, you'll have to use a generic formal package
> parameter:
>
> generic
> package Object_Package is new Generic_Obj_Package (...);
> package generic_pac ...
>
It seems to me you want as a generic paramter any type that has the Eval
operation.
So you can define a parent type having this operation (say an abstract type
called Evalable).
Now you can define the generic
with Evalable_Pak;
generic
type My_Evalable is new Evalable_Pak.Evalable with private;
....
My_Evalable will have a primitive Eval operation.
(Hope I am not making a fool of myself here. I confess not compiling a real
example.)
Ehud Lamm
(Stay tuned to the results of the Ada-Europe'2002 Workshop on a Standard
Container Library for Ada!)
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: class wide object in generic package
2002-06-11 20:57 ` Ehud Lamm
@ 2002-06-12 17:33 ` Stephen Leake
2002-06-13 1:58 ` Robert A Duff
0 siblings, 1 reply; 10+ messages in thread
From: Stephen Leake @ 2002-06-12 17:33 UTC (permalink / raw)
"Ehud Lamm" <mslamm@huji.ac.il> writes:
> It seems to me you want as a generic paramter any type that has the Eval
> operation.
> So you can define a parent type having this operation (say an abstract type
> called Evalable).
> Now you can define the generic
>
> with Evalable_Pak;
> generic
> type My_Evalable is new Evalable_Pak.Evalable with private;
> ....
>
> My_Evalable will have a primitive Eval operation.
Yes, it will have an Eval operation, but that operation will _not_ be
visible in the generic body. The generic body does _not_ "read" the
package spec for Evalable_Pak.
That's why you need the generic formal package parameter; then the
generic body _does_ see everything in the generic formal package spec.
> (Hope I am not making a fool of myself here.
Asking real questions is always ok.
> I confess not compiling a real example.)
That would have been a good idea :).
--
-- Stephe
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: class wide object in generic package
2002-06-12 17:33 ` Stephen Leake
@ 2002-06-13 1:58 ` Robert A Duff
2002-06-13 14:07 ` Stephen Leake
0 siblings, 1 reply; 10+ messages in thread
From: Robert A Duff @ 2002-06-13 1:58 UTC (permalink / raw)
Stephen Leake <stephen.a.leake.1@gsfc.nasa.gov> writes:
> "Ehud Lamm" <mslamm@huji.ac.il> writes:
>
> > It seems to me you want as a generic paramter any type that has the Eval
> > operation.
> > So you can define a parent type having this operation (say an abstract type
> > called Evalable).
> > Now you can define the generic
> >
> > with Evalable_Pak;
> > generic
> > type My_Evalable is new Evalable_Pak.Evalable with private;
> > ....
> >
> > My_Evalable will have a primitive Eval operation.
>
> Yes, it will have an Eval operation, but that operation will _not_ be
> visible in the generic body.
That's not correct. My_Evaluable inherits a primitive Eval from
Evalable_Pak.Evalable, and this may be called in the generic body.
In the instance, it will call the Eval for the actual type.
>... The generic body does _not_ "read" the
> package spec for Evalable_Pak.
It does.
> That's why you need the generic formal package parameter; then the
> generic body _does_ see everything in the generic formal package spec.
That's another way of doing things.
- Bob
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: class wide object in generic package
2002-06-13 1:58 ` Robert A Duff
@ 2002-06-13 14:07 ` Stephen Leake
0 siblings, 0 replies; 10+ messages in thread
From: Stephen Leake @ 2002-06-13 14:07 UTC (permalink / raw)
Robert A Duff <bobduff@shell01.TheWorld.com> writes:
> Stephen Leake <stephen.a.leake.1@gsfc.nasa.gov> writes:
> > Yes, it will have an Eval operation, but that operation will _not_ be
> > visible in the generic body.
>
> That's not correct. My_Evaluable inherits a primitive Eval from
> Evalable_Pak.Evalable, and this may be called in the generic body.
> In the instance, it will call the Eval for the actual type.
Sigh. I stand corrected. I should have followed my own advice and
written a small example program. Oh well ...
--
-- Stephe
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2002-06-13 14:07 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-06-10 7:38 class wide object in generic package Thomas Maier-Komor
2002-06-10 12:28 ` Craig Carey
2002-06-10 13:14 ` Thomas Maier-Komor
2002-06-10 13:00 ` Stephen Leake
2002-06-10 13:25 ` Thomas Maier-Komor
2002-06-11 20:57 ` Ehud Lamm
2002-06-12 17:33 ` Stephen Leake
2002-06-13 1:58 ` Robert A Duff
2002-06-13 14:07 ` Stephen Leake
-- strict thread matches above, loose matches on Subject: below --
2002-06-10 11:07 Grein, Christoph
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox