comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Question about generic child packages
Date: Thu, 13 Oct 2016 19:19:54 +0200
Date: 2016-10-13T19:19:54+02:00	[thread overview]
Message-ID: <ntofno$1kbu$1@gioia.aioe.org> (raw)
In-Reply-To: 08d81628-f1a6-430b-b023-0166a1bedfaa@googlegroups.com

On 2016-10-13 19:01, Charly wrote:

> I have the following question:
> Is it possible to have a generic parent package Parent with two (or more)
> child packages and than use objects defined in one of the children and use
> it in the other one?
> Following is a very simple example to show my problem.
> In Child_A I defined a function Double and want to use it in Child_B.
> It would also be nice to use objects in Child_A, that are defined in B,
> not shown in following example.
> Of course I can make Child_B a subchild of Child_A, but that is
> unsatisfying because it is asymmetric and does not solve the second
> part with mutual usage.
>
> --------------------
> generic
>     type Data is digits <>;
> package Parent is
>
> end Parent;
>
> --------------------
> generic
> package Parent.Child_A is
>
>    function Double
>      (X : in Data)
>       return Data
>    is
>      (2.0 * X);
>
> end Parent.Child_A;
>
> --------------------
> with Parent.Child_A;
>
> generic
> package Parent.Child_B is
>
>    function Test
>      (X : in Data)
>       return Data
>    is
>       (2.0 * Parent.Child_A.Double (X));
>
>
> end Parent.Child_B;

It does not work this way because generic children are themselves 
generic. There is no cascade instantiation of generics in Ada. Therefore 
you must pass an instance of Parent.Child_A to Parent.Child_B.

with Parent.Child_A;
generic
    with package A is new Parent.Child_A;
package Parent.Child_B is
    function Test (X : in Data) return Data is (2.0 * A.Double (X));
end Parent.Child_B;

The only way of having a cascade is nested packages. Nested generic 
packages are proper packages.

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

  reply	other threads:[~2016-10-13 17:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-13 17:01 Question about generic child packages Charly
2016-10-13 17:19 ` Dmitry A. Kazakov [this message]
2016-10-14  8:55   ` Charly
replies disabled

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