Search code examples
typesvisibilityprivateadagnat

Ada's private types visibility in implementation


Is a private type visible in the unit's implementation? For example, the .ads:

package Unlimited_Strings is
   type String_Block is private;
(...)
private
   Max_Block_Size : constant Integer := 10;
   subtype String_Size_Subtype is Integer range 0..Max_Block_Size;
   type String_Block(Size : String_Size_Subtype := Max_Block_Size) is record
      Next : Unlimited_String := null;        
      Str : String(1..Size) := (others => ' ');
   end record;

and the .adb:

Aux_Ptr := new String_Block(Max_Block_Size);

yield a compilation error: "constraint no allowed when type has a constrained partial view".


Solution

  • As Simon said, we need a reproducer here. I suspect the ".adb" file is not really the one corresponding to the .ads you are showing.

    Note that since you are using a default value for the discriminant, you are in effect not creating an unconstrained type. That means that the compiler will always allocate the maximal size for a String_Block (corresponding to a Max_Block_Size), even when you specify a much smaller value for Size. This might not be what you intended (this is a tricky area).

    On the other hand, since the public declaration for String_Block specifies that the type is constrained, you do have to specify the default value of course. Perhaps you meant:

       type String_Block (<>) is private;
    private
       type String_Block (Size : String_Size_Subtype) is record ...
    

    Again: your code is valid as far as we can tell, the above is just a suggestion on what might be better memory usage.