comp.lang.ada
 help / color / mirror / Atom feed
* Generic parent accessing his child?
@ 2014-12-06  1:36 hreba
  2014-12-06  7:49 ` Simon Wright
  0 siblings, 1 reply; 3+ messages in thread
From: hreba @ 2014-12-06  1:36 UTC (permalink / raw)


Without generics I had no problem letting a parent access his child, a 
"with" was enough. With generics I have the following problem:

-------------------------------------------------------

generic		-- a package used as generics parameter
package T is
    type	Ttype is private;
    procedure Init (t: in out Ttype);
private
    type Ttype is new Integer;
end T;

package body T is
    procedure Init (t: in out Ttype) is
    begin
       t:= 0;
    end Init;
end T;

--------------------------------------------------------

with T;		-- the parent
generic
    with package U is new T(<>);
package P is
    type AD is abstract tagged null record;
    type A is access all AD'Class;
    function NewA return A;
end P;

with P.Q;
package body P is
    function NewA return A is
    begin
       return A(P.Q.NewB);	--  <-- ERROR
    end NewA;
end P;
-------------------------------------------------------

generic		-- the child
package P.Q is
    type BD is new AD with
       record
          i:	U.Ttype;
       end record;
    type B is access all BD;
    function NewB return B;
end P.Q;

package body P.Q is
    function NewB return B is
       b0: B;
    begin
       b0:= new BD;
       U.Init(b0.i);
       return b0;
    end NewB;
end P.Q;

-----------------------------------------------------

The error message refers to the line marked above and reads:

Invalid prefix in selected component "Q".

What would be the correct way?

-- 
Frank Hrebabetzky		+55 / 48 / 3235 1106
Florianopolis, Brazil


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

* Re: Generic parent accessing his child?
  2014-12-06  1:36 Generic parent accessing his child? hreba
@ 2014-12-06  7:49 ` Simon Wright
  2014-12-07  1:13   ` hreba
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Wright @ 2014-12-06  7:49 UTC (permalink / raw)


hreba <hreba@terra.com.br> writes:

> Without generics I had no problem letting a parent access his child, a
> "with" was enough. With generics I have the following problem:
>
> -------------------------------------------------------
>
> generic		-- a package used as generics parameter
> package T is
>    type	Ttype is private;
>    procedure Init (t: in out Ttype);
> private
>    type Ttype is new Integer;
> end T;
>
> package body T is
>    procedure Init (t: in out Ttype) is
>    begin
>       t:= 0;
>    end Init;
> end T;
>
> --------------------------------------------------------
>
> with T;		-- the parent
> generic
>    with package U is new T(<>);
> package P is
>    type AD is abstract tagged null record;
>    type A is access all AD'Class;
>    function NewA return A;
> end P;
>
> with P.Q;
> package body P is
>    function NewA return A is
>    begin
>       return A(P.Q.NewB);	--  <-- ERROR
>    end NewA;
> end P;
> -------------------------------------------------------
>
> generic		-- the child
> package P.Q is
>    type BD is new AD with
>       record
>          i:	U.Ttype;
>       end record;
>    type B is access all BD;
>    function NewB return B;
> end P.Q;
>
> package body P.Q is
>    function NewB return B is
>       b0: B;
>    begin
>       b0:= new BD;
>       U.Init(b0.i);
>       return b0;
>    end NewB;
> end P.Q;
>
> -----------------------------------------------------
>
> The error message refers to the line marked above and reads:
>
> Invalid prefix in selected component "Q".
>
> What would be the correct way?

Compiled with -gnatwa (enables many additional warnings) gives

    22. with P.Q;
              |
        >>> warning: unit "P.Q" is never instantiated

    23. package body P is
    24.    function NewA return A is
    25.    begin
    26.       return A(P.Q.NewB);
                        |
        >>> invalid prefix in selected component "P.Q"

    27.    end NewA;
    28. end P;

because P.Q is itself generic.

You can get the code to compile by

   with P.Q;
   package body P is
      package My_Q is new P.Q;
      function NewA return A is
      begin
         return A(My_Q.NewB);
      end NewA;
   end P;

if that does what you want.

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

* Re: Generic parent accessing his child?
  2014-12-06  7:49 ` Simon Wright
@ 2014-12-07  1:13   ` hreba
  0 siblings, 0 replies; 3+ messages in thread
From: hreba @ 2014-12-07  1:13 UTC (permalink / raw)


Looked up this way of package instantiation in my Ada book. I had read 
over it before, but with a concrete problem in mind it is much clearer now.

Thanks.
-- 
Frank Hrebabetzky		+55 / 48 / 3235 1106
Florianopolis, Brazil


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

end of thread, other threads:[~2014-12-07  1:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-06  1:36 Generic parent accessing his child? hreba
2014-12-06  7:49 ` Simon Wright
2014-12-07  1:13   ` hreba

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