comp.lang.ada
 help / color / mirror / Atom feed
* Ada: Inheritance of generics
@ 2008-06-02 13:39 Dennis Hoppe
  2008-06-02 13:51 ` Dmitry A. Kazakov
  2008-06-02 14:30 ` Georg Bauhaus
  0 siblings, 2 replies; 8+ messages in thread
From: Dennis Hoppe @ 2008-06-02 13:39 UTC (permalink / raw)


Hi,

I managed to implement a generic parent class and create a generic unit, 
which is a child of the first one. I wonder, why I have access to 
procedures and functions of the parent class, if I am "within" the child 
unit, but if I am "outside", the compiler complains that no selector for 
a specified function exists.

-- generic parent
generic
   type Modular_Type is mod <>;
package Generic_Parent is
   type Object is abstract tagged limited null record;

   function Example return Integer;
end Generic_Parent;


-- child unit
generic
   -- omitted
package Generic_Child is
   type Object is new Generic_Parent.Object with private;

   function Example return Integer;
end Generic_Child;


-- testsuite
package Parent is new Generic_Parent (2**16);
package Child is new Parent.Object;

-- compiler error, no selector for ...
A : Integer := Child.Example;


My aim is to append some generic childs to a container and iterate
over each item and call a common procedure or function.


Best regards,
   Dennis Hoppe



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada: Inheritance of generics
  2008-06-02 13:39 Ada: Inheritance of generics Dennis Hoppe
@ 2008-06-02 13:51 ` Dmitry A. Kazakov
  2008-06-02 14:17   ` Dennis Hoppe
  2008-06-02 14:30 ` Georg Bauhaus
  1 sibling, 1 reply; 8+ messages in thread
From: Dmitry A. Kazakov @ 2008-06-02 13:51 UTC (permalink / raw)


On Mon, 02 Jun 2008 15:39:05 +0200, Dennis Hoppe wrote:

> I managed to implement a generic parent class and create a generic unit, 
> which is a child of the first one. I wonder, why I have access to 
> procedures and functions of the parent class, if I am "within" the child 
> unit, but if I am "outside", the compiler complains that no selector for 
> a specified function exists.
> 
> -- generic parent
> generic
>    type Modular_Type is mod <>;
> package Generic_Parent is
>    type Object is abstract tagged limited null record;
> 
>    function Example return Integer;
> end Generic_Parent;
> 
> -- child unit
> generic
>    -- omitted
> package Generic_Child is
>    type Object is new Generic_Parent.Object with private;

This is illegal. Generic_Parent is a generic unit, you cannot refer to
anything within it without either instantiation of, or else from a generic
child unit of. Generic_Child is not a child package of Generic_Parent.

P.S. Except for trivial cases, it is always better to post complete code
samples.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada: Inheritance of generics
  2008-06-02 13:51 ` Dmitry A. Kazakov
@ 2008-06-02 14:17   ` Dennis Hoppe
  2008-06-02 14:34     ` Georg Bauhaus
                       ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Dennis Hoppe @ 2008-06-02 14:17 UTC (permalink / raw)


Dmitry A. Kazakov wrote:
> On Mon, 02 Jun 2008 15:39:05 +0200, Dennis Hoppe wrote:
> 
>> -- child unit
>> generic
>>    -- omitted
>> package Generic_Child is
>>    type Object is new Generic_Parent.Object with private;
> 
> This is illegal. Generic_Parent is a generic unit, you cannot refer to
> anything within it without either instantiation of, or else from a generic
> child unit of. Generic_Child is not a child package of Generic_Parent.


Hmm...this line is really a left over from simple inheritance without 
generic unit, but the Ada compiler does not complain about it.

> P.S. Except for trivial cases, it is always better to post complete code
> samples.

I thought, this example was exhaustive: parent, child, instantiation.

Best regards,
   Dennis




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada: Inheritance of generics
  2008-06-02 13:39 Ada: Inheritance of generics Dennis Hoppe
  2008-06-02 13:51 ` Dmitry A. Kazakov
@ 2008-06-02 14:30 ` Georg Bauhaus
  1 sibling, 0 replies; 8+ messages in thread
From: Georg Bauhaus @ 2008-06-02 14:30 UTC (permalink / raw)


Dennis Hoppe schrieb:
> Hi,
> 
> I managed to implement a generic parent class and create a generic unit, 
> which is a child of the first one. I wonder, why I have access to 
> procedures and functions of the parent class, if I am "within" the child 
> unit, but if I am "outside", the compiler complains that no selector for 
> a specified function exists.

Do you mean a parent type and a derived type which happen
to be declared in package templates?

The name "generic child package" has a meaning in Ada that
does not necessarily have something to do with types.
Just in case you are used to thinking in C++, a package
is not always the same as a class. For example, you can
have two types within the same package.

Only types, i.e. things declared using the resered word
"type", can  derive from other types. Likewise, a
package instance is made from a generic package which
acts as a template for packages. But packages are not types.
So this does not work:

package Child is new Parent.Object;


function Example return Integer;
is not associated with type Object; its profile does
not mention Object.
Function Example is somewhat like a static member function
in C++.


  -- Georg



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada: Inheritance of generics
  2008-06-02 14:17   ` Dennis Hoppe
@ 2008-06-02 14:34     ` Georg Bauhaus
  2008-06-02 15:09     ` Dmitry A. Kazakov
  2008-06-02 15:13     ` Adam Beneschan
  2 siblings, 0 replies; 8+ messages in thread
From: Georg Bauhaus @ 2008-06-02 14:34 UTC (permalink / raw)


Dennis Hoppe schrieb:

> 
> I thought, this example was exhaustive: parent, child, instantiation.

Could you qualify these a little more? I.e., are you
referring to parent package and child package, or to parent
type and child type, or to how you intend these two concepts
to interact? (They aren't the same thing.)





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada: Inheritance of generics
  2008-06-02 14:17   ` Dennis Hoppe
  2008-06-02 14:34     ` Georg Bauhaus
@ 2008-06-02 15:09     ` Dmitry A. Kazakov
  2008-06-02 15:13     ` Adam Beneschan
  2 siblings, 0 replies; 8+ messages in thread
From: Dmitry A. Kazakov @ 2008-06-02 15:09 UTC (permalink / raw)


On Mon, 02 Jun 2008 16:17:48 +0200, Dennis Hoppe wrote:

> Dmitry A. Kazakov wrote:
>> On Mon, 02 Jun 2008 15:39:05 +0200, Dennis Hoppe wrote:
>> 
>>> -- child unit
>>> generic
>>>    -- omitted
>>> package Generic_Child is
>>>    type Object is new Generic_Parent.Object with private;
>> 
>> This is illegal. Generic_Parent is a generic unit, you cannot refer to
>> anything within it without either instantiation of, or else from a generic
>> child unit of. Generic_Child is not a child package of Generic_Parent.
> 
> Hmm...this line is really a left over from simple inheritance without 
> generic unit, but the Ada compiler does not complain about it.

Then get another compile. The following:

generic
   type Modular_Type is mod <>;
package Generic_Parent is
   type Object is abstract tagged limited null record;
   function Example return Integer;
end Generic_Parent;

with Generic_Parent;
generic
   -- omitted
package Generic_Child is
   type Object is new Generic_Parent.Object with private;
   function Example return Integer;
end Generic_Child;

compiled by GNAT ends up with::

generic_child.ads:5:23: invalid prefix in selected component
"Generic_Parent"

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada: Inheritance of generics
  2008-06-02 14:17   ` Dennis Hoppe
  2008-06-02 14:34     ` Georg Bauhaus
  2008-06-02 15:09     ` Dmitry A. Kazakov
@ 2008-06-02 15:13     ` Adam Beneschan
  2008-06-02 21:10       ` Dennis Hoppe
  2 siblings, 1 reply; 8+ messages in thread
From: Adam Beneschan @ 2008-06-02 15:13 UTC (permalink / raw)


On Jun 2, 7:17 am, Dennis Hoppe <dennis.ho...@hoppinet.de> wrote:
> Dmitry A. Kazakov wrote:
> > On Mon, 02 Jun 2008 15:39:05 +0200, Dennis Hoppe wrote:
>
> >> -- child unit
> >> generic
> >>    -- omitted
> >> package Generic_Child is
> >>    type Object is new Generic_Parent.Object with private;
>
> > This is illegal. Generic_Parent is a generic unit, you cannot refer to
> > anything within it without either instantiation of, or else from a generic
> > child unit of. Generic_Child is not a child package of Generic_Parent.
>
> Hmm...this line is really a left over from simple inheritance without
> generic unit, but the Ada compiler does not complain about it.
>
> > P.S. Except for trivial cases, it is always better to post complete code
> > samples.
>
> I thought, this example was exhaustive: parent, child, instantiation.

"Complete code sample" means something that you can compile.  In this
case, it's hard to tell what you're trying to do.  If your units are
all top-level library units, there's a lot of WITH's missing; plus
2**16 is not a type, so you can't instantiate Generic_Package with it,
and there's no completion for the private type extension Object,
you've tried to instantiate a type instead of a generic package.
Sometimes it's good enough to leave stuff out, but in this case I
can't tell what you're trying to do, and I suspect I'm not alone.

There's so many things wrong with this code that I'm just going to
take a guess at what you're trying to do and hope it's close to what
you're looking for.  If not, you'll need to explain more about what
you're trying to accomplish.  The following Ada source does compile.

generic
   type Modular_Type is mod <>;
package Generic_Parent is
   type Object is abstract tagged limited null record;

   function Example return Integer;
end Generic_Parent;

-- child unit
generic
   -- omitted
package Generic_Parent.Generic_Child is
   type Object is new Generic_Parent.Object with private;

   function Example return Integer;
private
   type Object is new Generic_Parent.Object with null record;
       -- or whatever you want in the type extension
end Generic_Parent.Generic_Child;

package Mod_Package is
   type Unsigned_16 is mod 2**16;
end Mod_Package;

with Generic_Parent;
with Mod_Package;
package Parent is new Generic_Parent (Mod_Package.Unsigned_16);

with Parent;
with Generic_Parent.Generic_Child;
package Child is new Parent.Generic_Child;

with Child;
package Pak1 is
   A : Integer := Child.Example;
end Pak1;

                          -- HTH, Adam



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada: Inheritance of generics
  2008-06-02 15:13     ` Adam Beneschan
@ 2008-06-02 21:10       ` Dennis Hoppe
  0 siblings, 0 replies; 8+ messages in thread
From: Dennis Hoppe @ 2008-06-02 21:10 UTC (permalink / raw)


Hello Adam,

Adam Beneschan wrote:
> There's so many things wrong with this code that I'm just going to
> take a guess at what you're trying to do and hope it's close to what
> you're looking for.  If not, you'll need to explain more about what
> you're trying to accomplish.  The following Ada source does compile.

Thank you for your effort. It does exactly, what I wanted to do. It 
seems, that I should read a bit more about inheritance und generics in 
Ada to understand this two different approaches of programming.

My own implementation failed due the wrong instantiation of the 
packages. Your last code snippets helped me the most.

> package Mod_Package is
>    type Unsigned_16 is mod 2**16;
> end Mod_Package;
> 
> with Generic_Parent;
> with Mod_Package;
> package Parent is new Generic_Parent (Mod_Package.Unsigned_16);
> 
> with Parent;
> with Generic_Parent.Generic_Child;
> package Child is new Parent.Generic_Child;
> 
> with Child;
> package Pak1 is
>    A : Integer := Child.Example;
> end Pak1;
> 
>                           -- HTH, Adam

Thank you,
   Dennis



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2008-06-02 21:10 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-02 13:39 Ada: Inheritance of generics Dennis Hoppe
2008-06-02 13:51 ` Dmitry A. Kazakov
2008-06-02 14:17   ` Dennis Hoppe
2008-06-02 14:34     ` Georg Bauhaus
2008-06-02 15:09     ` Dmitry A. Kazakov
2008-06-02 15:13     ` Adam Beneschan
2008-06-02 21:10       ` Dennis Hoppe
2008-06-02 14:30 ` Georg Bauhaus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox