comp.lang.ada
 help / color / mirror / Atom feed
* Passing an instance of a generic child as a generic parameter
@ 2008-06-14 20:54 Dennis Hoppe
  2008-06-15  8:57 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 4+ messages in thread
From: Dennis Hoppe @ 2008-06-14 20:54 UTC (permalink / raw)


Hi there,

I'm trying to pass an instance of a generic child unit to another 
generic package, but the only syntax, I could found specifies a concrete 
generic package.
Let's first have a look at my pathological example:

-- GENERIC PARENT 'other' ADS
generic
   B : Integer;
package Other is
   type Object is tagged null record;
   procedure Solve (Obj : Object);	
end Other;


-- GENERIC PARENT 'other' ADB
with Ada.Text_IO;

package body Other is
   procedure Solve (Obj : Object) is
   begin
     Ada.Text_IO.Put_Line ("solved");
   end;
end Other;


-- GENERIC CHILD of 'other' ADS
with Other;

generic
   -- omitted
package Other.Child is
   type Instance is new Other.Object with null record;	
	
   overriding
   procedure Solve (Obj : in Instance);
end Other.Child;


-- GENERIC CHILD of 'other' ADB
with Ada.Text_IO;

package body Other.Child is
   procedure Solve (Obj : in Instance) is
   begin
     Ada.Text_IO.Put ("other child");
   end;
end Other.Child;


-- GENERIC PARENT 'parent' ADS
with Other;

generic
   A: Integer;
   with package XX is new Other (A);
parent Parent is
   type Object is abstract tagged null record;
   procedure Print (Obj: in Object; X : in XX.Object'Class);
end Parent;


-- GENERIC PARENT 'parent' ADB
with Ada.Text_IO;

package body Parent is
   procedure Print (Obj : in Object; X : in XX.Object'Class) is
   begin
     X.Solve;
     Ada.Text_IO.Put (Integer'Image(A));
   end;
end Parent;

-- GENERIC CHILD of 'parent' ADS
with Parent;

generic
   -- omitted
	
package Parent.Child is
   type Instance is new Parent.Object with null record;
	
   overriding	
   procedure Print (Obj : in Instance; X : in XX.Object'Class);
end Parent.Child;


-- GENERIC CHILD of 'parent' ADB
with Ada.Text_IO;

package body Parent.Child is
   procedure Print (Obj : in Instance; X : in XX.Object'Class) is
   begin
     Ada.Text_IO.Put_Line ("child");
     X.Solve;
   end;	
end Parent.Child;


-- TESTSUITE
with Parent;
with Other;
with Other.Child;

procedure Test is
   A : Integer := 2;
   package O is new Other (A);
   package E is new O.Child;
   package P is new Parent (A, O);
		
   procedure Calls (Obj : P.Object'Class; X : in O.Object'Class) is		 
  begin -- Calls
       P.Print (Obj, X);
     end Calls;
	
   X : C.Instance;
   OO : O.Object;
   EE : E.Instance;
begin -- Test
   Calls (X, OO);
end Test;



Ok, if I declare 'package P is new Parent (A, O);' and call the Calls 
procedure with 'Calls (X, OO);' all went fine, because O is an instance 
of Other. I want further to declare my package P with a child unit of 
Other, so that:

procedure Test is
   A : Integer := 2;
   package O is new Other (A);
   package E is new O.Child;
   package P is new Parent (A, E);  -- O is replaced by the child E
   ..                               -- compiler error expected

end Test;

Is it possible to declare a generic package parameter to accept 
children, too? Or should I better solve this challenge with another 
approach?

generic
   A: Integer;
   with package XX is new Other (A);
   ..

... won't do it.


Thanks in advance,
   Dennis



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

* Re: Passing an instance of a generic child as a generic parameter
  2008-06-14 20:54 Passing an instance of a generic child as a generic parameter Dennis Hoppe
@ 2008-06-15  8:57 ` Dmitry A. Kazakov
  2008-06-15  9:04   ` Dmitry A. Kazakov
  0 siblings, 1 reply; 4+ messages in thread
From: Dmitry A. Kazakov @ 2008-06-15  8:57 UTC (permalink / raw)


On Sat, 14 Jun 2008 22:54:46 +0200, Dennis Hoppe wrote:

> I'm trying to pass an instance of a generic child unit to another 
> generic package, but the only syntax, I could found specifies a concrete 
> generic package.

Ada does not have interfaces of packages. A generic child package is not an
implementation of the parent's package interface, which could then be
referenced as a formal parameter of some other generic package. So the
answer is no.

But it seems to me that your example does not require that stuff:

> Let's first have a look at my pathological example:
> 
> -- GENERIC PARENT 'other' ADS
> generic
>    B : Integer;
> package Other is
>    type Object is tagged null record;
>    procedure Solve (Obj : Object);	
> end Other;
> 
> 
> -- GENERIC PARENT 'other' ADB
> with Ada.Text_IO;
> 
> package body Other is
>    procedure Solve (Obj : Object) is
>    begin
>      Ada.Text_IO.Put_Line ("solved");
>    end;
> end Other;
>  
> -- GENERIC CHILD of 'other' ADS
> with Other;
> 
> generic
>    -- omitted
> package Other.Child is
>    type Instance is new Other.Object with null record;	
> 	
>    overriding
>    procedure Solve (Obj : in Instance);
> end Other.Child;
> 
> -- GENERIC CHILD of 'other' ADB
> with Ada.Text_IO;
> 
> package body Other.Child is
>    procedure Solve (Obj : in Instance) is
>    begin
>      Ada.Text_IO.Put ("other child");
>    end;
> end Other.Child;
> 
> -- GENERIC PARENT 'parent' ADS
> with Other;
> 
> generic
>    A: Integer;
>    with package XX is new Other (A);

    type Object is new XX.Object with null record;

> parent Parent is
>    type Object is abstract tagged null record;

   remove this

>    procedure Print (Obj: in Object; X : in XX.Object'Class);
> end Parent;

Now Parent takes an instance of Other, which determines the class of
Objects to use. The class is parametrized by A. The formal type Object is
an instance from the class to deal with.

BTW, you could also remove the parameter A:

generic
   with package XX is new Other (<>); -- Brings some A with it
   type Object is new XX.Object with null record;

> parent Parent is

[...]

> procedure Test is
>    A : Integer := 2;
>    package O is new Other (A);
>    package E is new O.Child;
>    package P is new Parent (A, E);  -- O is replaced by the child E
>    ..                               -- compiler error expected

   package P is new Parent (A, O, E.Instance);

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



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

* Re: Passing an instance of a generic child as a generic parameter
  2008-06-15  8:57 ` Dmitry A. Kazakov
@ 2008-06-15  9:04   ` Dmitry A. Kazakov
  2008-06-15 12:04     ` Dennis Hoppe
  0 siblings, 1 reply; 4+ messages in thread
From: Dmitry A. Kazakov @ 2008-06-15  9:04 UTC (permalink / raw)


On Sun, 15 Jun 2008 10:57:44 +0200, Dmitry A. Kazakov wrote:

> On Sat, 14 Jun 2008 22:54:46 +0200, Dennis Hoppe wrote:
> 
>> I'm trying to pass an instance of a generic child unit to another 
>> generic package, but the only syntax, I could found specifies a concrete 
>> generic package.
> 
> Ada does not have interfaces of packages. A generic child package is not an
> implementation of the parent's package interface, which could then be
> referenced as a formal parameter of some other generic package. So the
> answer is no.
> 
> But it seems to me that your example does not require that stuff:
> 
>> Let's first have a look at my pathological example:
>> 
>> -- GENERIC PARENT 'other' ADS
>> generic
>>    B : Integer;
>> package Other is
>>    type Object is tagged null record;
>>    procedure Solve (Obj : Object);	
>> end Other;
>> 
>> 
>> -- GENERIC PARENT 'other' ADB
>> with Ada.Text_IO;
>> 
>> package body Other is
>>    procedure Solve (Obj : Object) is
>>    begin
>>      Ada.Text_IO.Put_Line ("solved");
>>    end;
>> end Other;
>>  
>> -- GENERIC CHILD of 'other' ADS
>> with Other;
>> 
>> generic
>>    -- omitted
>> package Other.Child is
>>    type Instance is new Other.Object with null record;	
>> 	
>>    overriding
>>    procedure Solve (Obj : in Instance);
>> end Other.Child;
>> 
>> -- GENERIC CHILD of 'other' ADB
>> with Ada.Text_IO;
>> 
>> package body Other.Child is
>>    procedure Solve (Obj : in Instance) is
>>    begin
>>      Ada.Text_IO.Put ("other child");
>>    end;
>> end Other.Child;
>> 
>> -- GENERIC PARENT 'parent' ADS
>> with Other;
>> 
>> generic
>>    A: Integer;
>>    with package XX is new Other (A);
> 
>     type Object is new XX.Object with null record;

   type Object is new XX.Object with private; -- Of course

>> parent Parent is
>>    type Object is abstract tagged null record;
> 
>    remove this
> 
>>    procedure Print (Obj: in Object; X : in XX.Object'Class);
>> end Parent;
> 
> Now Parent takes an instance of Other, which determines the class of
> Objects to use. The class is parametrized by A. The formal type Object is
> an instance from the class to deal with.
> 
> BTW, you could also remove the parameter A:
> 
> generic
>    with package XX is new Other (<>); -- Brings some A with it
>    type Object is new XX.Object with null record;
> 
>> parent Parent is
> 
> [...]
> 
>> procedure Test is
>>    A : Integer := 2;
>>    package O is new Other (A);
>>    package E is new O.Child;
>>    package P is new Parent (A, E);  -- O is replaced by the child E
>>    ..                               -- compiler error expected
> 
>    package P is new Parent (A, O, E.Instance);


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



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

* Re: Passing an instance of a generic child as a generic parameter
  2008-06-15  9:04   ` Dmitry A. Kazakov
@ 2008-06-15 12:04     ` Dennis Hoppe
  0 siblings, 0 replies; 4+ messages in thread
From: Dennis Hoppe @ 2008-06-15 12:04 UTC (permalink / raw)


Hello Dmitry,

thank you for your explanation and for some hints to correct my 
implementation. Now, it works just as I planned it.

Best regards,
   Dennis


Dmitry A. Kazakov wrote:
 > [flipped the long code-snippet]



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

end of thread, other threads:[~2008-06-15 12:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-14 20:54 Passing an instance of a generic child as a generic parameter Dennis Hoppe
2008-06-15  8:57 ` Dmitry A. Kazakov
2008-06-15  9:04   ` Dmitry A. Kazakov
2008-06-15 12:04     ` Dennis Hoppe

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