Evaluating default discriminant expressions 87-06-18 AI-00449/04 1 | !standard 03.03.02 (06) 87-06-18 AI-00449/04 | !class ramification 86-09-10 | !status approved by WG9/AJPO 87-06-17 | !status approved by Director, AJPO 87-06-17 | !status approved by WG9 87-05-29 !status approved by Ada Board (21-0-0) 87-02-19 !status panel/committee-approved 86-10-15 (reviewed) !status panel/committee-approved (8-0-0) 86-09-10 (pending editorial review) !status received 86-08-06 !references AI-00308, AI-00007, 83-00787 !topic Evaluating default discriminant expressions !summary 86-09-13 Default discriminant expressions are not evaluated when a subtype indication is elaborated. !question 86-09-13 Are default discriminant expressions evaluated (and checked for compatibility) when a subtype indication is elaborated? Consider the following examples: type R (D : INTEGER := -1) is record COMP : STRING (D .. 10); end record; type T1 is array (1..5) of R; -- CONSTRAINT_ERROR? (no) type T2 (D : BOOLEAN) is record C1 : R; -- CONSTRAINT_ERROR? (no) end record; OBJ1 : T1; -- CONSTRAINT_ERROR Is the default discriminant expression for R evaluated when the type declarations for T1 and T2 are elaborated, or is the default expression only evaluated (and checked for compatibility) when the object declaration is elaborated? !response 86-09-13 3.2.1(6) says: The elaboration of a subtype indication creates a subtype. If the subtype indication does not include a constraint, the subtype is the same as that denoted by the type mark. 3.2.1(6) goes on to say that if a constraint is present, the constraint is Evaluating default discriminant expressions 87-06-18 AI-00449/04 2 elaborated and then checked for compatibility. Hence, if no discriminant constraint is present, no default expressions are evaluated when a subtype indication is elaborated. In particular, when the type declarations given in the question are elaborated, no default discriminant expressions are evaluated. A default discriminant expression is only evaluated when an object is created. 3.2.1(6) says: If [an] object declaration includes an explicit initialization, the initial value is obtained by evaluating the corresponding expression. Otherwise any implicit initial values for the object or for its subcomponents are evaluated. 3.2.1(14) says: In the case of a component that is itself a composite object and whose value is defined neither by an explicit initialization nor by a default expression, any implicit initial values for components of the composite object are defined by the same rules as for a declared object. Hence, when the declaration of OBJ1 in the example is elaborated, 3.2.1(6 and 14) cause the default expression for the discriminant to be evaluated (once for each array component). Each default discriminant value is then checked for compatibility (AI-00308 requires the check; AI-00007 defines how to perform the check). In short, if a type with default discriminants is used without a discriminant constraint in a subtype indication, elaboration of the subtype indication does not cause the default expressions to be evaluated. However, if the subtype indication occurs in an object declaration that has no initialization expression, the default expressions will be evaluated as part of the object declaration's elaboration. (Similarly, occurrence of such a subtype indication in an allocator will cause the default expressions to be evaluated when the allocator is evaluated; see 4.8(6).)