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=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!news.albasani.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: formal array types and default values Date: Sun, 31 Dec 2017 00:20:39 +0200 Organization: Tidorum Ltd Message-ID: References: <053b2370-5c15-4662-a9e3-e1464de206a1@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net wBUjj8RzSkY/fKoIQAf34gFTIE/lMiiEl7H5bwO6URNAJSNLoG Cancel-Lock: sha1:UhIPt0ZniOI6UF9s/iIp0W823QI= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 In-Reply-To: <053b2370-5c15-4662-a9e3-e1464de206a1@googlegroups.com> Xref: reader02.eternal-september.org comp.lang.ada:49705 Date: 2017-12-31T00:20:39+02:00 List-Id: On 17-12-30 23:42 , Mehdi Saada wrote: > I have several questions more or less related. > First: why aspect DEFAULT_COMPONENT_VALUE can apply only > to components of scalar type ? Don't know. Perhaps the thought was that if the component is a record type, then there is already a way to give default values to the components of the record type. However, the AARM explains that this is a representation attribute, and why that is so, and (therefore?) requires the default component value to be defined by a /static/ expression, which means that it can only be a scalar or a string. In principle, a string might define the default component value of an array with elements of a constrained String subtype, but that is probably not a very common case. > It would be nice to be able to set a default value to an array type > in a generic package, equal to a generic object parameter. Agreed. Possibly this case was forgotten, or prevented by such a generic object parameter not being a static value. > type T_Vect_Coef is array (T_Degre range <>) of ELEMENT; > gives that message: > main.adb:21:22: unconstrained subtype not allowed (need initialization) > main.adb:21:22: provide initial value or explicit array bounds I'm pretty sure that these messages don't come from the type declaration, but from some place where you declare a variable of this type, without giving bounds or an initial value, as in: Coef : T_Vect_Coef; As the index range of T_Vect_Coef is not constrained, the index bounds of Coef would be indeterminate. The messages are telling you to either provide index bounds, as in: Coef : T_Vect_Coef (0 .. 42); or to provide an initial value that then defines the index bounds, as in: Coef : T_Vect_Coef := (0 => 23, 1 => 66, 2 .. 15 => 0); > ELEMENT and T_DEGRE are generic and declared like that: > type ELEMENT is private; > type T_DEGRE is range <>; > > Those two types are public. Huh? If these types are generic formal types, I do not understand what you mean by "public". > How can I give a default value, since > DEFAULT_COMPONENT_VALUE doesn't apply to non scalar types. > > In the private part, > the type > type T_Polynome (Degre : T_Degre := 0) is -- type mutant > record > COEF : T_Vect_Coef (0..Degre) := (others => NULL_ELEMENT); > end record; > gives that warning: creation of "T_Polynome" object may raise Storage_Error. Why? Usually this error message means the following: - As an initial value was provided for Degre, it is possible to define unconstrained variables of type T_Polynome, and such a variable should be able to handle any value of Degre. - In order to handle any value of Degre, the variable will be sized according to maximum size that can be required, which here occurs for Degre = T_Degre'Last. (Some compilers will allocate the COEF component on the heap, according to the actual value of Degre, and then will not have this problem. However, GNAT uses the allocate-maximum-size method.) - But T_Degre'Last is (or can be) so large that it will be impossible to create a T_Polynome (Degre => T_Degre'Last), and therefore Storage_Error will be raised. The remedy is to place som reasonable upper bound on the maximum possible value of Degre, by defining the type of Degree with a reasonable 'Last. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .