comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Generic parent accessing his child?
Date: Sat, 06 Dec 2014 07:49:41 +0000
Date: 2014-12-06T07:49:41+00:00	[thread overview]
Message-ID: <lyoarh2p62.fsf@pushface.org> (raw)
In-Reply-To: cef4t5Ff8n5U1@mid.individual.net

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.

  reply	other threads:[~2014-12-06  7:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-06  1:36 Generic parent accessing his child? hreba
2014-12-06  7:49 ` Simon Wright [this message]
2014-12-07  1:13   ` hreba
replies disabled

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