From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!GTEWD.AF.MIL!papayd From: papayd@GTEWD.AF.MIL ("14827_DAVID PAPAY") Newsgroups: comp.lang.ada Subject: Question about generics Message-ID: <8905292059.AA05478@ajpo.sei.cmu.edu> Date: 29 May 89 20:54:00 GMT Sender: usenet@ucbvax.BERKELEY.EDU Organization: The Internet List-Id: 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