Declaration of private type Containers

This is a discussion on Declaration of private type Containers within the ADA forums in Programming Languages category; 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 ...

Go Back   Application Development Forum > Programming Languages > ADA

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 05-30-2008, 07:49 AM
alexander.kleppe@web.de
Guest
 
Default Declaration of private type Containers

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
Reply With Quote
  #2  
Old 05-30-2008, 08:05 AM
Dmitry A. Kazakov
Guest
 
Default Re: Declaration of private type Containers

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
Reply With Quote
  #3  
Old 05-30-2008, 08:21 AM
Jean-Pierre Rosen
Guest
 
Default Re: Declaration of private type Containers

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
Reply With Quote
  #4  
Old 05-30-2008, 09:59 AM
alexander.kleppe@web.de
Guest
 
Default Re: Declaration of private type Containers

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.
Reply With Quote
  #5  
Old 05-30-2008, 10:25 AM
Jean-Pierre Rosen
Guest
 
Default Re: Declaration of private type Containers

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
Reply With Quote
  #6  
Old 05-30-2008, 12:00 PM
Adam Beneschan
Guest
 
Default Re: Declaration of private type Containers

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


Reply With Quote
  #7  
Old 05-30-2008, 04:27 PM
Matthew Heaney
Guest
 
Default Re: Declaration of private type Containers

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;

Reply With Quote
  #8  
Old 05-30-2008, 10:04 PM
Steve
Guest
 
Default Re: Declaration of private type Containers

<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.



Reply With Quote
  #9  
Old 05-31-2008, 01:28 AM
Randy Brukardt
Guest
 
Default Re: Declaration of private type Containers

"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.


Reply With Quote
  #10  
Old 06-04-2008, 11:40 AM
alexander.kleppe@web.de
Guest
 
Default Re: Declaration of private type Containers

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!
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:24 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.