TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy


Options: Use Forum View

Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Ben Brosgol <[log in to unmask]>
Reply To:
Ben Brosgol <[log in to unmask]>
Thu, 4 Feb 1999 11:23:58 -0500
text/plain (101 lines)
You can declare a private type with an "unknown discriminant part".  The
following example compiles and runs (I tried it under Aonix ObjectAda
package P is
    type T(<>) is private;
    function F(N : Integer) return T;
    type T is array(Positive range <>) of Integer;
end P;

package body P is
    function F(N : Integer) return T is
        return T'(1..N => 0);
    end F;
end P;

with P; use P;
procedure Test_Unconstrained is
    X : T := F(10);
end Test_Unconstrained;


Ben Brosgol
[log in to unmask]

-----Original Message-----
>From: Samuel Mize <[log in to unmask]>
To: [log in to unmask] <[log in to unmask]>
Date: Thursday, February 04, 1999 10:32 AM
Subject: Re: Problems with unconstrained arrays

>Rick Duley wrote:
>> 1.   It doesn't appear to be possible to compile the declaration of an
>> unconstrained type as either private or limited private.  I haven't been
>> able to find a prohibition of this in the LRM.  Is this a compiler
>> (GNAT 3.1)?
>I can't cite ARM chapter/verse, but I can see that this can't be done.
>The problem is that you could then try to declare a variable of the
>private type, but there's no way to give it its array bounds.  For
>    package A is
>      type A_Type is private;
>    private
>      type A_Type is array (integer range <>) of Integer;
>    end A;
>    -----
>    with A;
>    procedure Uses_A is
>      My_A_Type: A.A_Type; -- what are the array bounds?
>    ...
>One solution is to have the bounds (or just the upper bound) be
>discriminants to the private type.  This will force you to use a
>record type for the full view:
>    package A is
>        type A_Type (Count: Integer) is private;
>    private
>        type Int_Array is array (Integer range <>) of Integer;
>        type A_Type (Count: integer) is record
>           A_Field: Int_Array (1..Count);
>        end record;
>    end a;
>You could also have A_Type be an access to objects of type
>Int_Array.  That way you wouldn't need to have the array
>bounds be part of the public view.
>If this doesn't fill the bill, you may have to rethink your data
>design.  I'm not sure how you intend to use a private but
>unconstrained type.
>For chapter and verse, I suspect you should be checking for references
>about unconstrained or indefinite types.
>I hope this helps.
>Sam Mize
>Samuel Mize -- [log in to unmask] (home email) -- Team Ada
>Fight Spam: see \\\ Smert Spamonam