comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: formal array types and default values
Date: Sun, 31 Dec 2017 00:20:39 +0200
Date: 2017-12-31T00:20:39+02:00	[thread overview]
Message-ID: <faqhpnFljnfU1@mid.individual.net> (raw)
In-Reply-To: <053b2370-5c15-4662-a9e3-e1464de206a1@googlegroups.com>

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
       .      @       .

  reply	other threads:[~2017-12-30 22:20 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-30 21:42 formal array types and default values Mehdi Saada
2017-12-30 22:20 ` Niklas Holsti [this message]
2017-12-30 23:59   ` Mehdi Saada
2018-01-03  0:52   ` Randy Brukardt
2018-01-03  8:33     ` Niklas Holsti
2018-01-03 20:53       ` Randy Brukardt
2018-01-06  0:10         ` Niklas Holsti
2018-01-08 21:12           ` Randy Brukardt
2017-12-31 12:34 ` Jeffrey R. Carter
2017-12-31 14:55   ` Mehdi Saada
2018-01-03 15:47 ` Mehdi Saada
2018-01-03 21:16   ` Randy Brukardt
2018-01-04 15:02 ` Mehdi Saada
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox