6.8 Expression Functions
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
expression_function_declaration provides a shorthand to declare a function whose body consists of a single return statement.
Name Resolution Rules3/4
expression_function_declaration that is not a completion declares an expression function. The return expression of an expression function is the
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.
A potentially static expression is defined in the same way as a static expression except that
- a name denoting a formal parameter of an expression function is a potentially static expression; and
- 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”.
The following language-defined representation aspect may be specified for an expression function:
- 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_definitionshall be a static expression.
- 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.
[A static expression function is a static function; see 4.9.]
expression_function_declaration is a completion, it shall be the completion of a
generic_subprogram_declaration. The profile of an
expression_function_declaration that completes a declaration shall conform fully to that of the declaration.
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
aggregate of the
expression_function_declaration, shall not be statically deeper than that of the master that elaborated the
Aspect Static shall be specified to have the value True only if the associated
- is not a completion;
- has an
expressionthat is a potentially static expression; 5.4/5
- contains no calls to itself;
- each parameter (if any) is of mode in and is of a static subtype;
- has a result subtype that is a static subtype;
- has no applicable precondition or postcondition expression; and
- 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.
Paragraph 6 was deleted.
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
expression is the return expression of the expression function.
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.
Example of an expression function:
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
Extensions to Ada 2012
aggregatecan directly be the return expression of an expression function. This eliminates the double parentheses that otherwise would be necessary.