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.3 required=5.0 tests=BAYES_00,HK_RANDOM_FROM, INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,48bf593f723b7524 X-Google-Attributes: gid103376,public From: bglbv@my-dejanews.com Subject: Re: Can I have an array of variant records? Date: 1999/04/20 Message-ID: <87aew3j899.fsf@bglbv.my-dejanews.com>#1/1 X-Deja-AN: 468882029 References: <371c84fb.0@silver.truman.edu> X-Complaints-To: abuse@freeuk.net X-Trace: mors.clara.net 924644098 212.126.147.97 (Tue, 20 Apr 1999 22:34:58 BST) NNTP-Posting-Date: Tue, 20 Apr 1999 22:34:58 BST Newsgroups: comp.lang.ada Date: 1999-04-20T00:00:00+00:00 List-Id: "Josh Highley" writes: > I have the following type declaration and I would like to have an array of > them. However, I need "length" to have different values for each element of > the array. > > type field (length : positive) is limited private; 3.6(10) says that "the subtype defined by the subtype_indication of a component_definition (the component subtype) shall be a definite subtype." So you have to make up your mind as to the value of the discriminant when you declare your array. > --it's later defined to be a record with a string(1..length), among other > information. > > For instance, I would like field_array(1) to be a field with a length of 10, > field_array(2) to be a field of length 50, and so on. Can I declare an > array of these fields, with each field of a different length? Not that I can see, but you could make an array of access values instead. Alternatively, you could get rid of the discriminant by using something like a bounded string of a suitably chosen maximum length. Or by making your record contain an access-to-String field instead of a String: since the type is limited, you don't have to worry about users of the package getting mixed up between shallow and deep copies. (Otherwise you might have needed to make the type controlled.) I find Ada's rules reasonably intuitive if I adopt a model in which all components of an array must have the same storage size. In the case of a record with a component of type String(1..Length), the storage size of the record will depend on the value of Length (unless the implementation decides to make that component a hidden pointer, which is something I wouldn't want it to be forced to). So if you want to make an array of these things, you'd better somehow arrange for all components of the array to have the same value of the Length discriminant. Or else sweep the size variability under the rug by adding a level of indirection through access values. And if I had to choose, I'd rather do that under cover of the "limited private", i.e. by resorting to type String_Access is access String; type Field is record -- Note: no discriminant ... Label : String_Access; -- (the length is stored here) ... end record; rather than to type Field_Access is access Field; type Field_Array is array (Index_Type range <>) of Field_Access; But either way will "work" in some sense.