[comp.lang.ada] Question about generics

GDAU100@BGUVM.BITNET ("Jonathan B. Owen") (05/29/89)

A while back I tried to define the following:

   generic
       Max_len : Integer;
   package VSTR is

      subtype Len_range is integer range 0..Max_len;

      type V_string( len : Len_range := 0 ) is
          record
             data : String(1..len);
          end record;

      ----------------------
      -- VSTRING SERVICES --
      ----------------------

      function V_string_of( str : in String ) return V_string;

      Etc...
end VSTR;

The purpose of such a package is obviously to use Variable length
strings having different maximum lengths.

Using the about definition after an instantiation on Vax-Ada worked fine
(Such as "package VSTR80 is new VSTR(80)" ).

When transferring the code to Verdix Ada 3.0, on the apollo (SR 10.1),
I received a compiler warning on a variable of such a type, saying
that there is not enough storage for such a variable.
It seems that the upper limit of len_range is not considered constant...

Any ideas to overcome this difficulty?

                                             Thank you,
                                                       JB

______________________________________________________________________________
  (--)    /--)     /-(\                 Email: gdau100@bguvm (bitnet)
  \ /    /--K      | \|/\   /\/) /|-\   Snail: 55 Hovevei Zion
  _/_/o /L__)_/o \/\__/  \X/  \_/ | |_/        Tel-Aviv, 63346  ISRAEL
 (/        Jonathan B. Owen             Voice: (03) 281-422

 Point of view:  A chicken is the means by which an egg reproduces an egg.
______________________________________________________________________________

papayd@GTEWD.AF.MIL ("14827_DAVID PAPAY") (05/30/89)

In issue #141, Jonathan Owen presents the following generic unit and asks:

>   generic
>       Max_len : Integer;
>   package VSTR is
>
>      subtype Len_range is integer range 0..Max_len;
>
>      type V_string( len : Len_range := 0 ) is
>          record
>             data : String(1..len);
>          end record;
>
>      ----------------------
>      -- VSTRING SERVICES --
>      ----------------------
>
>      function V_string_of( str : in String ) return V_string;
>
>      Etc...
>end VSTR;
>
>
>When transferring the code to Verdix Ada 3.0, on the apollo (SR 10.1),
>I received a compiler warning on a variable of such a type, saying
>that there is not enough storage for such a variable.
>It seems that the upper limit of len_range is not considered constant...
>
>Any ideas to overcome this difficulty?


First of all, the difficulty has nothing to do with the fact the generics are
being used.  Instead, it arises because of the use of a default expression
for the discriminant in the record type definition:
>
>      type V_string( len : Len_range := 0 ) is
>          record
>             data : String(1..len);
>          end record;
>

Since the discriminant has a default expression, it becomes possible to declare
record objects in two ways:

MY_STRING_A  : V_STRING (20);  -- with an explicit discriminant constraint

MY_STRING_B  : V_STRING;       -- using the default expression of 0.

The first record object is said to be CONSTRAINED.  The value of the
discriminant LEN cannot be changed, (not even with a complete record 
assignment), and evaluation of the attribute MY_STRING_A'CONSTRAINED will be 
TRUE.  Because the language rules do not allow MY_STRING_A.LEN to be changed,
the compiler can allocate the exact (minimum) storage space needed to represent
the object.

In the other hand, the record object MY_STRING_B is UNCONSTRAINED (evaluation
of MY_STRING_B'CONSTRAINED will be FALSE).  Because no explicit discriminant 
constraint was given, the value of MY_STRING_B.LEN can be changed during program
execution (using a complete record assignment only).  Since the discriminant can
change, the compiler cannot allocate the exact storage space needed, but must
allocate enough space to accommodate the largest possible value of LEN.  In 
this case, the record object must be large enough to hold a component DATA of
type STRING with a range of 1..INTEGER'LAST.  Depending on the value of
INTEGER'LAST, this can be quite a long string!

If you wish to use this package on Verdix Ada, I would suggest that you remove 
the default expression " := 0 " from your record type definition and always 
declare record objects of this type with explicit discriminant constraints.


David Papay                         papayd@gtewd.af.mil
GTE Government Systems           
PO Box 7188  M/S 5G09               voice: (415) 694-1522
Mountain View, Ca 94039