Annotated Ada Reference Manual (Ada 202y Draft 1)Legal Information
Contents   Index   References   Search   Previous   Next 

6.8 Expression Functions

1/3
{AI05-0177-1} An expression_function_declaration provides a shorthand to declare a function whose body consists of a single return statement. 

Syntax

2/4
{AI95-0177-1} {AI12-0157-1} expression_function_declaration ::= 
   [overriding_indicator]
   function_specification is
       (expression)
       [aspect_specification];
 | [overriding_indicator]
   function_specification is
       aggregate
       [aspect_specification];

Name Resolution Rules

3/4
{AI05-0177-1} {AI12-0157-1} The expected type for the expression or aggregate of an expression_function_declaration is the result type (see 6.5) of the function.

Static Semantics

3.1/5
 {AI05-0177-1} {AI05-0264-1} {AI12-0075-1} {AI12-0157-1} {AI12-0408-1} An expression_function_declaration that is not a completion declares an expression function. The return expression of an expression function is the expression or aggregate of the expression_function_declaration. A completion is not allowed for an expression_function_declaration; however, an expression_function_declaration can complete a previous declaration.
3.2/5
 {AI12-0075-1} A potentially static expression is defined in the same way as a static expression except that
3.3/5
a name denoting a formal parameter of an expression function is a potentially static expression; and
3.4/5
each use of “static expression” in the definition of “static expression” is replaced with a corresponding use of “potentially static expression” in the definition of “potentially static expression”. 
3.a/5
Discussion: These uses occur in the definition of “static expression” in the cases of function calls, type conversions, qualified expressions, membership tests, short circuit control forms, conditional expressions, and parenthesized expressions. 
3.5/5
 {AI12-0075-1} The following language-defined representation aspect may be specified for an expression function:
3.6/5
 Static
The type of aspect Static is Boolean. When aspect Static is True for an expression function, the function is a static expression function. If directly specified, the aspect_definition shall be a static expression.
3.b/5
Aspect Description for Static: Specifies that an associated expression function can be used in static expressions.
3.7/5
 
The Static value for an inherited function is True if some corresponding primitive function of the parent or progenitor type is a static expression function; otherwise, if not directly specified, the aspect is False.
3.8/5
 {AI12-0075-1} [A static expression function is a static function; see 4.9.]

Legality Rules

4/3
{AI05-0177-1} If an expression_function_declaration is a completion, it shall be the completion of a subprogram_declaration or generic_subprogram_declaration. The profile of an expression_function_declaration that completes a declaration shall conform fully to that of the declaration.
5/4
{AI05-0177-1} {AI12-0157-1} If the result subtype has one or more unconstrained access discriminants, the accessibility level of the anonymous access type of each access discriminant, as determined by the expression or aggregate of the expression_function_declaration, shall not be statically deeper than that of the master that elaborated the expression_function_declaration.
5.a/3
Ramification: This can only fail if the discriminant is an access to a part of a nonaliased parameter, as there can be no local declarations here. 
5.b/4
Discussion: {AI12-0005-1} We don't need to repeat any of the other Legality Rules for return statements since none of them can fail here: the implicit return statement has to apply to this function (and isn't nested in something), there clearly is a return statement in this function, and the static class-wide accessibility check cannot fail as a tagged type cannot be declared locally in an expression function. 
5.1/5
 {AI12-0075-1} Aspect Static shall be specified to have the value True only if the associated expression_function_declaration:
5.2/5
is not a completion;
5.3/5
has an expression that is a potentially static expression;
5.4/5
contains no calls to itself;
5.5/5
each parameter (if any) is of mode in and is of a static subtype;
5.6/5
has a result subtype that is a static subtype;
5.7/5
has no applicable precondition or postcondition expression; and
5.8/5
{AI12-0075-1} {AI12-0191-1} for result type R, if the function is a boundary entity for type R (see 7.3.2), no type invariant applies to type R; if R has a component type C, a similar rule applies to C
5.c/5
Ramification: Since a string subtype can be static, this allows an expression function of a string type to be static. 
Paragraph 6 was deleted.

Dynamic Semantics

7/5
{AI05-0177-1} {AI05-0262-1} {AI12-0157-1} {AI12-0408-1} The execution of an expression function is invoked by a subprogram call. For the execution of a subprogram call on an expression function, or on a function completed with a expression_function_declaration, the execution of the subprogram_body executes an implicit function body containing only a simple_return_statement whose expression is the return expression of the expression function.
7.a/3
Discussion: The last sentence effectively means that all of the dynamic wording in 6.5 applies as needed, and we don't have to repeat it here. 
8/3
{AI05-0177-1} The elaboration of an expression_function_declaration has no other effect than to establish that the expression function can be called without failing the Elaboration_Check. 

Examples

9/5
{AI12-0429-1} Example of an expression function: 
10/3
{AI05-0177-1} function Is_Origin (P : in Point) return Boolean is -- see 3.9
   (P.X = 0.0 and P.Y = 0.0);

Extensions to Ada 2005

10.a/3
{AI05-0177-1} Expression functions are new in Ada 2012. 

Extensions to Ada 2012

10.b/4
{AI12-0157-1} Corrigendum: A aggregate can directly be the return expression of an expression function. This eliminates the double parentheses that otherwise would be necessary.
10.c/5
{AI12-0075-1} Aspect Static is new; it allows using suitable expression functions in static expressions. 

Wording Changes from Ada 2012

10.d/5
{AI12-0408-1} Clarified the term “expression function” so it matches the meaning expected in 6.1

Contents   Index   References   Search   Previous   Next 
Ada-Europe Ada 2005 and 2012 Editions sponsored in part by Ada-Europe