comp.lang.ada
 help / color / mirror / Atom feed
* Re: Calling generic children from their parent?
  1999-06-23  0:00 ` Dale Stanbrough
@ 1999-06-23  0:00   ` rcollinson
  1999-06-24  0:00     ` Tucker Taft
  0 siblings, 1 reply; 8+ messages in thread
From: rcollinson @ 1999-06-23  0:00 UTC (permalink / raw)





In article
<dale-2306991457170001@dale.cs.rmit.edu.au>,
  dale@cs.rmit.edu.au (Dale Stanbrough) wrote:
> rcollinson@aol.com wrote:
>
> > Problem:
> > I need to use a type in a generic parent body
that is located in the generic
> > child specification.  I am having problems
instantiating the child in the
> > parent's body.  The type is not recognized
unless it is instantiated.  Any
> > ideas?
>
> You have to instantiate all of the generics that
you want to use. Maybe
> you have done this, but your example was not
sufficient to show this.
>
> Dale
>

My problem is that I TRY to instantiate a generic
child unit inside the body of the parent and it
will not compile.  The text Rendezvous with Ada 95
states this is possible on page 337 but I have
been unable to make it work.  (On a slightly
different subject, why in the example on page 337
do they WITH and ELABORATE Generic_Parent.Child in
the last exerpt?)

Roger


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




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

* Calling generic children from their parent?
@ 1999-06-23  0:00 RCollinson
  1999-06-23  0:00 ` Dale Stanbrough
  1999-06-23  0:00 ` Matthew Heaney
  0 siblings, 2 replies; 8+ messages in thread
From: RCollinson @ 1999-06-23  0:00 UTC (permalink / raw)


Problem:
I need to use a type in a generic parent body that is located in the generic
child specification.  I am having problems instantiating the child in the
parent's body.  The type is not recognized unless it is instantiated.  Any
ideas?

generic
type A;
type B;
package foo is
  type My_Type is tagged private;
  procedure Do_This;
  procedure Do_That;
...
  private
  type My_Type is
  record
...
  end record;
end foo;

with foo.bar;
package body foo is
  My_Var : Foo.Bar.My_Extended_Type;  -- !!! How do I get an instance of this
variable
                                                             -- !!! that is a
type from the generic child???
                                                             -- !!! Is this
possible???
end foo;
-- ********************************************************8
generic
package foo.bar is
   type My_Extended_Type is new My_Type with
   record
...
   end record;
...
end foo.bar;




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

* Re: Calling generic children from their parent?
  1999-06-23  0:00 Calling generic children from their parent? RCollinson
  1999-06-23  0:00 ` Dale Stanbrough
@ 1999-06-23  0:00 ` Matthew Heaney
  1 sibling, 0 replies; 8+ messages in thread
From: Matthew Heaney @ 1999-06-23  0:00 UTC (permalink / raw)


On 23 Jun 1999 04:42, rcollinson@aol.com (RCollinson) wrote:

> I need to use a type in a generic parent body that is located in the
> generic child specification.

This is a strange way to organize a hierarchy of generic packages.
Perhaps we can find a better organization that avoids these problems
altogether.

> I am having problems instantiating the child in the parent's body.
> The type is not recognized unless it is instantiated.  Any ideas?

How is that different from any other generic?  To make an entity
provided by a generic available for use, you have to instantiate the
generic.

foo.bar is a generic, and so to use the types declared there, foo.bar
must first be instantiated.


> with foo.bar;
> package body foo is
>   My_Var : Foo.Bar.My_Extended_Type;  
>   -- !!! How do I get an instance of this variable
>   -- !!! that is a type from the generic child???
>   -- !!! Is this possible???
>
> end foo;

You have to instantiate generic package foo.bar.


> generic
> package foo.bar is
>    type My_Extended_Type is new My_Type with
>    record
> ...
>    end record;
> ...
> end foo.bar;


Why does this need to be in a (generic) child of foo?  Why can't it go
in the public or private region of foo?  (There's no technical reason it
can't, because foo.bar doesn't import any generic actual parameters.)

Move My_Extended_Type to foo, and get rid of foo.bar.  









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

* Re: Calling generic children from their parent?
  1999-06-23  0:00 Calling generic children from their parent? RCollinson
@ 1999-06-23  0:00 ` Dale Stanbrough
  1999-06-23  0:00   ` rcollinson
  1999-06-23  0:00 ` Matthew Heaney
  1 sibling, 1 reply; 8+ messages in thread
From: Dale Stanbrough @ 1999-06-23  0:00 UTC (permalink / raw)


rcollinson@aol.com wrote:

> Problem:
> I need to use a type in a generic parent body that is located in the generic
> child specification.  I am having problems instantiating the child in the
> parent's body.  The type is not recognized unless it is instantiated.  Any
> ideas?


You have to instantiate all of the generics that you want to use. Maybe
you have done this, but your example was not sufficient to show this.

Dale




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

* Re: Calling generic children from their parent?
  1999-06-23  0:00   ` rcollinson
@ 1999-06-24  0:00     ` Tucker Taft
  1999-06-24  0:00       ` rcollinson
  0 siblings, 1 reply; 8+ messages in thread
From: Tucker Taft @ 1999-06-24  0:00 UTC (permalink / raw)


rcollinson@my-deja.com wrote:
> ...
> My problem is that I TRY to instantiate a generic
> child unit inside the body of the parent and it
> will not compile.  The text Rendezvous with Ada 95
> states this is possible on page 337 but I have
> been unable to make it work.  (On a slightly
> different subject, why in the example on page 337
> do they WITH and ELABORATE Generic_Parent.Child in
> the last exerpt?)

If you can post a small self-contained example with the
accompanying error message you received from the compiler,
I suspect someone could quickly point out the solution to your problem.

Here is an example that should work:

   generic
   package P is 
       procedure Dummy;
   end P;

   generic
   package P.C is
       type T is null record;
   end P.C;

   with P.C;
   package body P is
      package Child is new P.C;
      type NT is new Child.T;
      procedure Dummy is
          X : NT;
      begin
          null;
      end;
     -- ...
   end P;

Note that you normally have to instantiate all ancestors
of a child of a generic before instantiating the child.  
However, when inside the parent generic, the name of the 
parent generic itself represents the "current instance",
and hence you don't need to instantiate the parent generic inside
itself to be able to instantiate the child.

> Roger

-- 
-Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
Technical Director, Distributed IT Solutions  (www.averstar.com/tools)
AverStar (formerly Intermetrics, Inc.)   Burlington, MA  USA




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

* Re: Calling generic children from their parent?
  1999-06-24  0:00     ` Tucker Taft
@ 1999-06-24  0:00       ` rcollinson
  1999-06-25  0:00         ` Tucker Taft
  0 siblings, 1 reply; 8+ messages in thread
From: rcollinson @ 1999-06-24  0:00 UTC (permalink / raw)


In article <37723E5B.53577638@averstar.com>,
  Tucker Taft <stt@averstar.com> wrote:
> rcollinson@my-deja.com wrote:
> > ...
> > My problem is that I TRY to instantiate a generic
> > child unit inside the body of the parent and it
> > will not compile.  The text Rendezvous with Ada 95
> > states this is possible on page 337 but I have
> > been unable to make it work.  (On a slightly
> > different subject, why in the example on page 337
> > do they WITH and ELABORATE Generic_Parent.Child in
> > the last exerpt?)
>
> If you can post a small self-contained example with the
> accompanying error message you received from the compiler,
> I suspect someone could quickly point out the solution to your
problem.
>
> Here is an example that should work:
>
>    generic
>    package P is
>        procedure Dummy;
>    end P;
>
>    generic
>    package P.C is
>        type T is null record;
>    end P.C;
>
>    with P.C;
>    package body P is
>       package Child is new P.C;
>       type NT is new Child.T;
>       procedure Dummy is
>           X : NT;
>       begin
>           null;
>       end;
>      -- ...
>    end P;
>
> Note that you normally have to instantiate all ancestors
> of a child of a generic before instantiating the child.
> However, when inside the parent generic, the name of the
> parent generic itself represents the "current instance",
> and hence you don't need to instantiate the parent generic inside
> itself to be able to instantiate the child.
>
> > Roger
>
> --
> -Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
> Technical Director, Distributed IT Solutions  (www.averstar.com/tools)
> AverStar (formerly Intermetrics, Inc.)   Burlington, MA  USA
>

The problem was that I was using a tagged type.  Code follows:

generic
package P is
 type T is tagged
   record
     A : integer;
   end record;
  procedure Dummy;
end P;

generic
package P.C is
  type T is new P.T with
    record
      B : integer;
    end record;
end P.C;

with P.C;
package body P is
  package Child is new P.C;  --  This does not compile.
  type Nt is new Child.T;
  procedure Dummy is
    X : Nt;
  begin
    null;
  end Dummy;
end P;

-- Compilation error is:
-- Child is illegal because the type extension P.Child.T shall not be
declared in a generic body if the parent type is declared outside that
body.

*** Any ideas how to get around this???  Thanks!!!


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




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

* Re: Calling generic children from their parent?
  1999-06-24  0:00       ` rcollinson
@ 1999-06-25  0:00         ` Tucker Taft
  1999-07-01  0:00           ` rcollinson
  0 siblings, 1 reply; 8+ messages in thread
From: Tucker Taft @ 1999-06-25  0:00 UTC (permalink / raw)


rcollinson@my-deja.com wrote:
> ...
> The problem was that I was using a tagged type.  Code follows:
> 
> generic
> package P is
>  type T is tagged
>    record
>      A : integer;
>    end record;
>   procedure Dummy;
> end P;
> 
> generic
> package P.C is
>   type T is new P.T with
>     record
>       B : integer;
>     end record;
> end P.C;
> 
> with P.C;
> package body P is
>   package Child is new P.C;  --  This does not compile.
>   type Nt is new Child.T;
>   procedure Dummy is
>     X : Nt;
>   begin
>     null;
>   end Dummy;
> end P;
> 
> -- Compilation error is:
> -- Child is illegal because the type extension P.Child.T shall not be
> declared in a generic body if the parent type is declared outside that
> body.
> 
> *** Any ideas how to get around this???  Thanks!!!

You will need to move all of your type extensions into the (private
part of the) generic spec.

One way you can accomplish that is to change P.C from
being a child to being a separate generic, with a formal tagged type
as a parameter.  E.g.:

 generic
   type P_T is tagged private;
   --... Other formal params as necessary to make up for not being a child
 package P_C is
   type T is new P_T with
     record
       B : integer;
     end record;
 end P_C;

 with P_C;
 generic
 package P is
   type T is tagged
    record
      A : integer;
    end record;
   procedure Dummy;
 private
   package Child is new P_C(T);
   type Nt is new Child.T with null record;
 end P;
 
 package body P is
   procedure Dummy is
     X : Nt;
   begin
     null;
   end Dummy;
 end P;

   
-- 
-Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
Technical Director, Distributed IT Solutions  (www.averstar.com/tools)
AverStar (formerly Intermetrics, Inc.)   Burlington, MA  USA




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

* Re: Calling generic children from their parent?
  1999-06-25  0:00         ` Tucker Taft
@ 1999-07-01  0:00           ` rcollinson
  0 siblings, 0 replies; 8+ messages in thread
From: rcollinson @ 1999-07-01  0:00 UTC (permalink / raw)


In article <3773C090.F177F565@averstar.com>,
  Tucker Taft <stt@averstar.com> wrote:
> rcollinson@my-deja.com wrote:
> > ...
> > The problem was that I was using a tagged type.  Code follows:
> >
> > generic
> > package P is
> >  type T is tagged
> >    record
> >      A : integer;
> >    end record;
> >   procedure Dummy;
> > end P;
> >
> > generic
> > package P.C is
> >   type T is new P.T with
> >     record
> >       B : integer;
> >     end record;
> > end P.C;
> >
> > with P.C;
> > package body P is
> >   package Child is new P.C;  --  This does not compile.
> >   type Nt is new Child.T;
> >   procedure Dummy is
> >     X : Nt;
> >   begin
> >     null;
> >   end Dummy;
> > end P;
> >
> > -- Compilation error is:
> > -- Child is illegal because the type extension P.Child.T shall not
be
> > declared in a generic body if the parent type is declared outside
that
> > body.
> >
> > *** Any ideas how to get around this???  Thanks!!!
>
> You will need to move all of your type extensions into the (private
> part of the) generic spec.
>
> One way you can accomplish that is to change P.C from
> being a child to being a separate generic, with a formal tagged type
> as a parameter.  E.g.:
>
>  generic
>    type P_T is tagged private;
>    --... Other formal params as necessary to make up for not being a
child
>  package P_C is
>    type T is new P_T with
>      record
>        B : integer;
>      end record;
>  end P_C;
>
>  with P_C;
>  generic
>  package P is
>    type T is tagged
>     record
>       A : integer;
>     end record;
>    procedure Dummy;
>  private
>    package Child is new P_C(T);
>    type Nt is new Child.T with null record;
>  end P;
>
>  package body P is
>    procedure Dummy is
>      X : Nt;
>    begin
>      null;
>    end Dummy;
>  end P;
>
> --
> -Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
> Technical Director, Distributed IT Solutions  (www.averstar.com/tools)
> AverStar (formerly Intermetrics, Inc.)   Burlington, MA  USA
>


Thanks for the help with this!

I used a private child class with private operations to implement the
solution.  It uses private dispatching which I found to be pretty cool.
I made a public class-wide create function that returns a class object
the caller can pass to the public operations and unknown to him will
dispatch if necessary.

Thanks again!


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




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

end of thread, other threads:[~1999-07-01  0:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-06-23  0:00 Calling generic children from their parent? RCollinson
1999-06-23  0:00 ` Dale Stanbrough
1999-06-23  0:00   ` rcollinson
1999-06-24  0:00     ` Tucker Taft
1999-06-24  0:00       ` rcollinson
1999-06-25  0:00         ` Tucker Taft
1999-07-01  0:00           ` rcollinson
1999-06-23  0:00 ` Matthew Heaney

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