Re: Discriminants.
Laurent GUERBY <[email protected]>
| Newsgroups | gmane.comp.lang.ada.france |
|---|---|
| Message-ID | <[email protected]> |
On Sun, 2010-02-21 at 11:36 +0100, Pascal wrote:
> Bonjour, petite question sur les discriminants:
> Je souhaite écrire:
> type TÉléments is array (Natural range <>) of Integer;
> type Typ (Max : Natural := 256) is record
> Éléments : TÉléments(0..Max-1):=(others=>0);
> end record;
> X : Typ(20);
> Mais GNAT me renvoi :
> "discriminant in constraint must appear alone"
>
> Bien, je n'ai pas trouvé cette contrainte dans le § "3.7
> Discriminants" du manuel, mais les exemples proposés sont conformes.
>
> Bien, je pourrais évidemment déclarer:
> type TÉléments is array (Natural range <>) of Integer;
> type Typ (Max : Natural := 255) is record
> Éléments : TÉléments(0..Max):=(others=>0);
> end record;
> X : Typ(20-1);
>
> Mais, pourquoi ne pas pouvoir écrire Max-1 ?
Sauf erreur cette contrainte sur l'usage d'un discriminant est specifiee
dans 3.8(12) "its name shall appear alone as a direct_name". La raison
donnee dans les annotations est de ne pas complexifier les
implementations plus que necessaire.
Si tu as besoin de plusieurs tailles dependantes dans ton record tu peux
rajouter des discriminants en les documentant : "Max_Moins_1 :
Natural := 254", pas tres pratique mais ca marche.
Laurent
http://guerby.org/blog
<<
3.8 Record Types
...
12 A name that denotes a noninherited discriminant is allowed within the
declaration of the type, but not within the discriminant_part. If the
discriminant is used to define the constraint of a component, the bounds
of an entry family, or the constraint of the parent subtype in a
derived_type_definition then its name shall appear alone as a
direct_name (not as part of a larger expression or expanded name). A
discriminant shall not be used to define the constraint of a scalar
component.{discriminant (use in a record definition)}
12.a Reason: The penultimate restriction simplifies implementation, and
allows the outer discriminant and the inner discriminant or bound to
possibly share storage.
12.b Ramification: Other rules prevent such a discriminant from being an
inherited one.
12.c Reason: The last restriction is inherited from Ada 83. The
restriction is not really necessary from a language design point of
view, but we did not remove it, in order to avoid unnecessary
changes to existing compilers.
12.d Discussion: Note that a discriminant can be used to define the
constraint for a component that is of an access-to-composite type.
12.e/2 Reason: {AI95-00373-01} The above rules, and a similar one in 6.1
for formal parameters, are intended to allow initializations of
components or parameters to occur in a (nearly) arbitrary order -
whatever order is most efficient (subject to the restrictions of
3.3.1), since one default_expression cannot depend on the value of
another one. They also prevent circularities.
12.f Ramification: Inherited discriminants are not allowed to be denoted,
except within representation items. However, the
discriminant_selector_name of the parent subtype_indication is allowed to denote
a discriminant of the parent.
>>
_______________________________________________
Site WWW de l'association Ada-France: http://www.ada-france.org/
[email protected]
http://www.ada-france.org/mailman/listinfo/ada-france