Ada Conformity Assessment Authority      Home Conformity Assessment   Test Suite ARGAda Standard
Annotated Ada Reference ManualLegal Information
Contents   Index   References   Search   Previous   Next 

6.8 Expression Functions

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


{AI95-0177-1} {AI95-0147-1} expression_function_declaration ::= 
   function_specification is
 | [overriding_indicator]
   function_specification is

Name Resolution Rules

{AI05-0177-1} {AI95-0147-1} The expected type for the expression or aggregate of an expression_function_declaration is the result type (see 6.5) of the function.

Legality Rules

{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.
{AI05-0177-1} {AI95-0147-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 expression function, shall not be statically deeper than that of the master that elaborated the expression_function_declaration.
Ramification: This can only fail if the discriminant is an access to a part of a non-aliased parameter, as there can be no local declarations here. 
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 classwide accessibility check cannot fail as a tagged type cannot be declared locally in an expression function. 

Static Semantics

{AI05-0177-1} {AI05-0264-1} {AI95-0147-1} An expression_function_declaration 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. 

Dynamic Semantics

{AI05-0177-1} {AI05-0262-1} {AI95-0147-1} The execution of an expression function is invoked by a subprogram call. For the execution of a subprogram call on an expression function, the execution of the subprogram_body executes an implicit function body containing only a simple_return_statement whose expression is the return expression that of the expression function.
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. 
{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. 


{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

{AI05-0177-1} Expression functions are new in Ada 2012. 

Extensions to Ada 2012

{AI12-0157-1} A aggregate can directly be the return expression of an expression function. This eliminates the double parentheses that otherwise would be necessary.

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