comp.lang.ada
 help / color / mirror / Atom feed
* Declaration of private type Containers
@ 2008-05-30 11:49 alexander.kleppe
  2008-05-30 12:05 ` Dmitry A. Kazakov
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: alexander.kleppe @ 2008-05-30 11:49 UTC (permalink / raw)


Hi all,

I'm relatively new to Ada. I have a problem, which seems pretty basic
to me. Thus, I guess you guys have a easy solution ready.
The only thing I want to do is to  declare a list package, whose
Element_Type is of a private record type:

package A is

type My_Type is private

package My_Type_Lists is new Ada.Containers.Doubly_Linked_List
     (Element_Type	=> My_Type);

private

type My_Type is
record
...
end record;

end A;


GNAT compiler says, this is a "premature use of private type", which I
accept. However, I don't wanna declare the My_Type_Lists package
elsewhere, which would be a possible solution.
Ideally I wanna use it in some other Package or subprogram like this:

package B is

type Rec_Type is private

package My_Type_Lists is new Ada.Containers.Doubly_Linked_List
     (Element_Type	=> A.My_Type);
-- would be possible, but not very nice

private

type Rec_Type is
record
A_List : A.My_Type_Lists.List;
end record;


So what is the correct way to declare the Container package in package
A?

Thanks in advance,
Alex



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

* Re: Declaration of private type Containers
  2008-05-30 11:49 Declaration of private type Containers alexander.kleppe
@ 2008-05-30 12:05 ` Dmitry A. Kazakov
  2008-05-30 12:21 ` Jean-Pierre Rosen
  2008-05-30 20:27 ` Matthew Heaney
  2 siblings, 0 replies; 10+ messages in thread
From: Dmitry A. Kazakov @ 2008-05-30 12:05 UTC (permalink / raw)


On Fri, 30 May 2008 04:49:41 -0700 (PDT), alexander.kleppe@web.de wrote:

> The only thing I want to do is to  declare a list package, whose
> Element_Type is of a private record type:
 
package A is
   type My_Type is private
private
   type My_Type is record
      ...
   end record;
end A;

package A.Doubly_Linked_List is -- Child instance
   new Ada.Containers.Doubly_Linked_List (My_Type);

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



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

* Re: Declaration of private type Containers
  2008-05-30 11:49 Declaration of private type Containers alexander.kleppe
  2008-05-30 12:05 ` Dmitry A. Kazakov
@ 2008-05-30 12:21 ` Jean-Pierre Rosen
  2008-05-30 13:59   ` alexander.kleppe
  2008-05-31  5:28   ` Randy Brukardt
  2008-05-30 20:27 ` Matthew Heaney
  2 siblings, 2 replies; 10+ messages in thread
From: Jean-Pierre Rosen @ 2008-05-30 12:21 UTC (permalink / raw)


alexander.kleppe@web.de a �crit :
> Hi all,
> 
> I'm relatively new to Ada. I have a problem, which seems pretty basic
> to me. Thus, I guess you guys have a easy solution ready.
> The only thing I want to do is to  declare a list package, whose
> Element_Type is of a private record type:
> 
> package A is
> 
> type My_Type is private
> 
> package My_Type_Lists is new Ada.Containers.Doubly_Linked_List
>      (Element_Type	=> My_Type);
> 
> private
> 
> type My_Type is
> record
> ...
> end record;
> 
> end A;
> 
> 
> GNAT compiler says, this is a "premature use of private type", which I
> accept. 
Glad to hear that you accept it. The ARG tried hard to allow something 
like this, and eventually gave up under the pressure of an exponentially 
growing mountain of cans of worms...

If My_Type_Lists is not used within My_Type itself, the easiest way is 
to instantiate it as a child package:
package A.My_Type_Lists is new Ada.Containers.Doubly_Linked_List
       (Element_Type	=> My_Type);

-- 
---------------------------------------------------------
            J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr



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

* Re: Declaration of private type Containers
  2008-05-30 12:21 ` Jean-Pierre Rosen
@ 2008-05-30 13:59   ` alexander.kleppe
  2008-05-30 14:25     ` Jean-Pierre Rosen
                       ` (2 more replies)
  2008-05-31  5:28   ` Randy Brukardt
  1 sibling, 3 replies; 10+ messages in thread
From: alexander.kleppe @ 2008-05-30 13:59 UTC (permalink / raw)


Thanks so far.
So if I understand you right, there is no way to declare the list
package within the spec file of My_Type beside declaring My_Type
public? I suppose I have to source out the subprograms using the list
instances in their parameter list out of package A as well...
So my solution now is a new package spec and body called A.Containers,
which contains all the container packages and subprograms formerly
declared in A (which brought the error with private types). The
alternative would be your suggestion to declare the package as
A.My_Types_List in package B. Hope that's what you meant, otherwise
please correct me.



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

* Re: Declaration of private type Containers
  2008-05-30 13:59   ` alexander.kleppe
@ 2008-05-30 14:25     ` Jean-Pierre Rosen
  2008-05-30 16:00     ` Adam Beneschan
  2008-05-31  2:04     ` Steve
  2 siblings, 0 replies; 10+ messages in thread
From: Jean-Pierre Rosen @ 2008-05-30 14:25 UTC (permalink / raw)


alexander.kleppe@web.de a �crit :
> Thanks so far.
> So if I understand you right, there is no way to declare the list
> package within the spec file of My_Type beside declaring My_Type
> public? 
Think about it. If you were allowed to do that, the full declaration
of My_Type could include a list - and you could end up with a record 
that contains itself...

> I suppose I have to source out the subprograms using the list
> instances in their parameter list out of package A as well...
> So my solution now is a new package spec and body called A.Containers,
> which contains all the container packages and subprograms formerly
> declared in A (which brought the error with private types). The
> alternative would be your suggestion to declare the package as
> A.My_Types_List in package B. Hope that's what you meant, otherwise
> please correct me.
A.My_Types_List cannot be in package B, it has to be a compilation unit.

But if you have other operations that need list, you could have a child 
package that contains the instantiation of lists + other operations on 
lists.

-- 
---------------------------------------------------------
            J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr



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

* Re: Declaration of private type Containers
  2008-05-30 13:59   ` alexander.kleppe
  2008-05-30 14:25     ` Jean-Pierre Rosen
@ 2008-05-30 16:00     ` Adam Beneschan
  2008-05-31  2:04     ` Steve
  2 siblings, 0 replies; 10+ messages in thread
From: Adam Beneschan @ 2008-05-30 16:00 UTC (permalink / raw)


On May 30, 6:59 am, alexander.kle...@web.de wrote:
> Thanks so far.
> So if I understand you right, there is no way to declare the list
> package within the spec file of My_Type beside declaring My_Type
> public?

You could use a nested package, although I'm not sure if this would
give you what you want:

   package A is

       package Nested is
          type My_Type is private;
       private
          type My_Type is ...
       end Nested;

       subtype My_Type is Nested.My_Type;
       package My_Type_Lists is new Ada.Containers.Doubly_Linked_List
          (Element_Type      => My_Type);

   end A;

This is legal, and My_Type_Lists is now inside A as you want, but now
only the body of A.Nested will have access to the full definition of
Nested.  Maybe it could be made to work, though.

                             -- Adam





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

* Re: Declaration of private type Containers
  2008-05-30 11:49 Declaration of private type Containers alexander.kleppe
  2008-05-30 12:05 ` Dmitry A. Kazakov
  2008-05-30 12:21 ` Jean-Pierre Rosen
@ 2008-05-30 20:27 ` Matthew Heaney
  2008-06-04 15:40   ` alexander.kleppe
  2 siblings, 1 reply; 10+ messages in thread
From: Matthew Heaney @ 2008-05-30 20:27 UTC (permalink / raw)


On May 30, 7:49 am, alexander.kle...@web.de wrote:
> The only thing I want to do is to  declare a list package, whose
> Element_Type is of a private record type:
>
> package A is
>
> type My_Type is private
>
> package My_Type_Lists is new Ada.Containers.Doubly_Linked_List
>      (Element_Type      => My_Type);
>
> private
>
> type My_Type is
> record
> ...
> end record;
>
> end A;

It's unlikely you need all of the features supported by the standard
linked list container.  If so, one alternative for you would be to
create a more high level container:

package A is
  type T is private;

  type Container is tagged limited private;

  procedure Insert
   (C : in out Container;
    Obj : T);

  procedure Remove
    (C : in out Container;
     Obj : T);

  procedure Iterate
    (C : Container;
     P : not null access procedure (Obj : T));

private
   type T is ...;

   package T_Lists is new Ada.C.DLLists (T);

   type Container is tagged limited record
     L : T_Lists.List;
   end Container;

end A;




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

* Re: Declaration of private type Containers
  2008-05-30 13:59   ` alexander.kleppe
  2008-05-30 14:25     ` Jean-Pierre Rosen
  2008-05-30 16:00     ` Adam Beneschan
@ 2008-05-31  2:04     ` Steve
  2 siblings, 0 replies; 10+ messages in thread
From: Steve @ 2008-05-31  2:04 UTC (permalink / raw)


<alexander.kleppe@web.de> wrote in message 
news:8910e931-889f-4617-a249-71b5016a13e2@r66g2000hsg.googlegroups.com...
> Thanks so far.
> So if I understand you right, there is no way to declare the list
> package within the spec file of My_Type beside declaring My_Type
> public? ...

That depends on how you define public.  I don't think Ada has a definition 
of public.

In Ada if you're going to use a type it must be visible to the code that 
uses it.  For private types, to be able to use the type the declaration must 
be visible, but not the definition.  So when you have:

package A is

  type My_Record is private;
  package My_List is new ListPackage( My_Record );

private

  type My_Record is
    record
        field : Field_Type;
    end record;

end A;

In order to define MyList as a list of My_Record's, My_Record must be 
visible, but the defintion of My_Record may still be private.

In essance users of Package A will know that My_Record exists, and they will 
know that My_List is a generic package that is instantiated for My_Record, 
buty they won't know what My_Record looks like (they can't see into the 
private section).

You may already understand this, but I wasn't sure by your post.

Regards,
Steve
(The Duck)

> ... I suppose I have to source out the subprograms using the list
> instances in their parameter list out of package A as well...
> So my solution now is a new package spec and body called A.Containers,
> which contains all the container packages and subprograms formerly
> declared in A (which brought the error with private types). The
> alternative would be your suggestion to declare the package as
> A.My_Types_List in package B. Hope that's what you meant, otherwise
> please correct me. 





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

* Re: Declaration of private type Containers
  2008-05-30 12:21 ` Jean-Pierre Rosen
  2008-05-30 13:59   ` alexander.kleppe
@ 2008-05-31  5:28   ` Randy Brukardt
  1 sibling, 0 replies; 10+ messages in thread
From: Randy Brukardt @ 2008-05-31  5:28 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 862 bytes --]

"Jean-Pierre Rosen" <rosen@adalog.fr> wrote in message 
news:9hro1g.ni4.ln@hunter.axlog.fr...
> alexander.kleppe@web.de a �crit :
...
>> GNAT compiler says, this is a "premature use of private type", which I
>> accept.
> Glad to hear that you accept it. The ARG tried hard to allow something 
> like this, and eventually gave up under the pressure of an exponentially 
> growing mountain of cans of worms...

I'm still of the opinion that we (the ARG) gave up too soon on this problem. 
In any case, it's still on the ARG's radar (see AI05-0074-1 and AI05-0074-2) 
although looking at extensions is not a high priority right now.

In any case, it isn't allowed in any version of Ada. You'll have to use 
another (less elegant) solution, such as the child package or reexported 
operations suggested by others.

                             Randy Brukardt.





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

* Re: Declaration of private type Containers
  2008-05-30 20:27 ` Matthew Heaney
@ 2008-06-04 15:40   ` alexander.kleppe
  0 siblings, 0 replies; 10+ messages in thread
From: alexander.kleppe @ 2008-06-04 15:40 UTC (permalink / raw)


this is really a nice and elegant solution to my problem.
In particular because the former solution (list declaration in child
package of A) don't work, when I have another type in the same package
A which has such a list instance as one of its record components. With
your solution, it works perfect and it is transparent to the user of
the package.

Thanks to all for the answers!



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

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

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-30 11:49 Declaration of private type Containers alexander.kleppe
2008-05-30 12:05 ` Dmitry A. Kazakov
2008-05-30 12:21 ` Jean-Pierre Rosen
2008-05-30 13:59   ` alexander.kleppe
2008-05-30 14:25     ` Jean-Pierre Rosen
2008-05-30 16:00     ` Adam Beneschan
2008-05-31  2:04     ` Steve
2008-05-31  5:28   ` Randy Brukardt
2008-05-30 20:27 ` Matthew Heaney
2008-06-04 15:40   ` alexander.kleppe

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