comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <Stephen.Leake@gsfc.nasa.gov>
Subject: Re: Private declaration question
Date: 1997/06/10
Date: 1997-06-10T00:00:00+00:00	[thread overview]
Message-ID: <339D5973.3953@gsfc.nasa.gov> (raw)
In-Reply-To: 865912531.32snx@jvdsys.nextjk.stuyts.nl


Jerry van Dijk wrote:
> 
> Why has the completion of a private declaration to be a full view ?
> 
>      1. package Oops is
>      2.
>      3.    type A_Type is limited private;
>      4.
>      5. private
>      6.
>      7.    type A_Type is array (Positive range <>) of Integer;
>                 |
>         >>> full view of type must be definite subtype
> 
> After browsing the RM I think the error message comes from rule
> 7.3(4). If that is true, why is this ? It seems a logical construction
> to protect A_Type objects from user interference.

Actually, the error is due to RM 7.3(12); the partial view of A_Type has
no discriminants, so the full view must be a definite subtype. A
definite subtype has a known size; an unconstrained array is not a
definite subtype (as the error message says).

The reason is that the compiler must know how much memory to allocate
when a user of A_Type declares an object:

	My_Object : Oops.A_Type;

Oops provides no way to set the size of the array. The only way to fix
this, while keeping A_Type limited, is to provide a discriminant:

   package Oops is
      type A_Type (Size : Positive) is limited private;

   private
      type Array_Type is array (Positive range <>) of Integer;

      type A_Type (Size : Positive) is record
         Contents : Array_Type (1 .. Size);
      end record;
   end Oops;

Now the user can declare an object:

   A : Oops.A_Type (2);

If A_Type does not need to be limited, you can set the size by returning
a value from an initialization function:

   package Oops is
      type A_Type (<>) is private;

      function Init (Size : in Positive) return A_Type;
   private
      type A_Type is array (Positive range <>) of Integer;

   end Oops;

   package body Oops is
      function Init (Size : in Positive) return A_Type
      is begin
         return (1 .. Size => 0);
      end Init;
   end Oops;

   A : Oops.A_Type := Oops.Init (2);

-- 
- Stephe




  parent reply	other threads:[~1997-06-10  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-06-10  0:00 Private declaration question Jerry van Dijk
1997-06-10  0:00 ` Dale Stanbrough
1997-06-11  0:00   ` Jerry van Dijk
1997-06-10  0:00     ` John G. Volan
1997-06-10  0:00       ` John G. Volan
1997-06-10  0:00 ` Anonymous
1997-06-10  0:00 ` Stephen Leake [this message]
1997-06-10  0:00 ` Robert A Duff
replies disabled

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