comp.lang.ada
 help / color / mirror / Atom feed
* Question about generic child packages
@ 2016-10-13 17:01 Charly
  2016-10-13 17:19 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 3+ messages in thread
From: Charly @ 2016-10-13 17:01 UTC (permalink / raw)


Hi,

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;


Charly


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

* Re: Question about generic child packages
  2016-10-13 17:01 Question about generic child packages Charly
@ 2016-10-13 17:19 ` Dmitry A. Kazakov
  2016-10-14  8:55   ` Charly
  0 siblings, 1 reply; 3+ messages in thread
From: Dmitry A. Kazakov @ 2016-10-13 17:19 UTC (permalink / raw)


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

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

* Re: Question about generic child packages
  2016-10-13 17:19 ` Dmitry A. Kazakov
@ 2016-10-14  8:55   ` Charly
  0 siblings, 0 replies; 3+ messages in thread
From: Charly @ 2016-10-14  8:55 UTC (permalink / raw)


Thank you for the answer. I suspected this but I wanted to confirm it.

Regards
Charly


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

end of thread, other threads:[~2016-10-14  8:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-13 17:01 Question about generic child packages Charly
2016-10-13 17:19 ` Dmitry A. Kazakov
2016-10-14  8:55   ` Charly

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