Copyright © 2018 Oracle America, Inc. Legal Notice.
This document proposes changes to the Java Language Specification to allow use of the var
keyword with lambda parameters. See JEP 323 for an overview.
The formal parameters of a method or constructor, if any, are specified by a list of comma-separated parameter specifiers. Each parameter specifier consists of a type (optionally preceded by the final
modifier and/or one or more annotations) and an identifier (optionally followed by brackets) that specifies the name of the parameter.
If a method or constructor has no formal parameters, only an empty pair of parentheses appears in the declaration of the method or constructor.
FormalParameterList:
ReceiverParameter
FormalParameters,
LastFormalParameter
LastFormalParameter
FormalParameter {,
FormalParameter }
FormalParameters:
FormalParameter {,
FormalParameter }
ReceiverParameter {,
FormalParameter }
FormalParameter:
{ VariableModifier } UnannType VariableDeclaratorId
ReceiverParameter
VariableArityParameter
VariableModifier:
Annotation
final
ReceiverParameter:
{ Annotation } UnannType [ Identifier
.
]this
LastFormalParameter:VariableArityParameter:{ VariableModifier } UnannType { Annotation }
...
VariableDeclaratorIdIdentifier
FormalParameter
The following productions from 4.3 and 8.3 are shown here for convenience:
VariableDeclaratorId:
Identifier
[
Dims]
Dims:
{ Annotation }
[
]
{ { Annotation }[
]
}
The use of var
with lambda parameters introduces yet another variation on possible syntactic forms in a parameter list. A lot of complexity in the grammar comes from its attempts to control the order in which these syntactic forms can appear. As a simplifying refactoring, the grammars for parameter lists have been rearranged and no longer enforce ordering. Instead, these constraints are enforced with semantic rules, described below.
The last formal parameter of a method or constructor is special: it may be a variable arity parameter, indicated by an ellipsis following the type.
Note that the ellipsis (
...
) is a token unto itself (3.11). It is possible to put whitespace between it and the type, but this is discouraged as a matter of style.
If the last formal parameter is a variable arity parameter, the method is a variable arity method. Otherwise, it is a fixed arity method.
The receiver parameter is an optional syntactic device for an instance method or an inner class's constructor. For an instance method, the receiver parameter represents the object for which the method is invoked. For an inner class's constructor, the receiver parameter represents the immediately enclosing instance of the newly constructed object. Either way, the receiver parameter exists solely to allow the type of the represented object to be denoted in source code, so that the type may be annotated. The receiver parameter is not a formal parameter; more precisely, it is not a declaration of any kind of variable (4.12.3), it is never bound to any value passed as an argument in a method invocation expression or qualified class instance creation expression, and it has no effect whatsoever at run time.
The rules for annotation modifiers on a formal parameter declaration and on a receiver parameter are specified in 9.7.4 and 9.7.5.
It is a compile-time error if final
appears more than once as a modifier for a formal parameter declaration.
It is a compile-time error to use mixed array notation (10.2) for a variable arity parameter.
This rule is now enforced by the grammar.
It is a compile-time error if a VariableArityParameter appears in a FormalParameterList in a position other than the last element of the list.
The scope and shadowing of a formal parameter is specified in 6.3 and 6.4.
It is a compile-time error for a method or constructor to declare two formal parameters with the same name. (That is, their declarations mention the same Identifier.)
It is a compile-time error if a formal parameter that is declared final
is assigned to within the body of the method or constructor.
A receiver parameter may appear only in the FormalParameterList of an instance method or an inner class's constructor; otherwise, a compile-time error occurs.
It is a compile-time error if a ReceiverParameter appears in a FormalParameterList in a position other than the first element of the list.
Where a receiver parameter is allowed, its type and name are specified as follows:
...
The formal parameters of a lambda expression may have either declared types or inferred types be expressed as either a list of comma-separated parameter specifiers or a list of comma-separated identifiers. In the latter case, if there is just one identifier, the parentheses surrounding the list may be elided. These styles cannot be mixed: it is not possible for a lambda expression to declare the types of some of its parameters but leave others to be inferred. Only parameters with declared types can have modifiers. Modifiers may not be applied to the elements of an identifier list.
LambdaParameters:
Identifier
(
[FormalParameterListLambdaParameterList ])
(
InferredFormalParameterListIdentifierList)
Identifier
LambdaParameterList:
LambdaParameter {
,
LambdaParameter }
LambdaParameter:
{ VariableModifier } LambdaParameterType VariableDeclaratorId
VariableArityParameter
LambdaParameterType:
UnannType
var
InferredFormalParameterList:IdentifierList:Identifier {
,
Identifier }
The following productions from 4.3, 8.3, and 8.4.1 are shown here for convenience:
FormalParameterList:
ReceiverParameter
FormalParameters,
LastFormalParameter
LastFormalParameter
FormalParameters:
FormalParameter {,
FormalParameter }
ReceiverParameter {,
FormalParameter }
FormalParameter:
{ VariableModifier } UnannType VariableDeclaratorId
LastFormalParameter:VariableArityParameter:{ VariableModifier } UnannType { Annotation }
...
VariableDeclaratorIdIdentifier
FormalParameterVariableModifier:
Annotation
final
VariableDeclaratorId:
Identifier
[
Dims]
Dims:
{ Annotation }
[
]
{ { Annotation }[
]
}
Receiver parameters are not permitted in the FormalParameters of a lambda expression, as specified in 8.4.1.
This rule is now enforced by the grammar.
If a formal parameter of a lambda expression lacks a declared type, because it is declared either in an identifier list or with a parameter specifier that uses var
in place of a type, the parameter's type will be inferred (15.27.3) from the functional interface type targeted by the lambda expression.
A lambda expression whose formal parameters have declared types is said to be explicitly typed, while a lambda expression whose formal parameters have inferred types is said to be implicitly typed. A lambda expression with zero parameters is explicitly typed.
It is a compile-time error if the parameter list of a lambda expression includes both a parameter with a declared type and a parameter with an inferred type.
If the formal parameters have inferred types, then these types are derived (15.27.3) from the functional interface type targeted by the lambda expression.
The syntax for formal parameters with declared types is the same as the syntax for the parameters of a method declaration (8.4.1).
The declared type of a formal parameter depends on whether it is a variable arity parameter (8.4.1):
If the formal parameter is not a variable arity parameter, then the declared type is denoted by UnannType if no bracket pairs appear in UnannType and VariableDeclaratorId, and specified by 10.2 otherwise.
If the formal parameter is a variable arity parameter, then the declared type is specified by 10.2. (Note that "mixed notation" is not permitted for variable arity parameters.)
No distinction is made between the following lambda parameter lists:
(int... x) -> .. (int[] x) -> ..
Consistent with the rules for overriding, either can be used, whether the functional interface's abstract method is fixed arity or variable arity. Since lambda expressions are never directly invoked, introducing
int...
where the functional interface usesint[]
can have no impact on the surrounding program. In a lambda body, a variable arity parameter is treated just like an array-typed parameter.
The rules for annotation modifiers on a formal parameter declaration are specified in 9.7.4 and 9.7.5.
It is a compile-time error if final
appears more than once as a modifier for a formal parameter declaration.
It is a compile-time error to use mixed array notation (10.2) for a variable arity parameter.
This rule is now enforced by the grammar.
It is a compile-time error if a VariableArityParameter appears in a LambdaParameterList in a position other than the last element of the list.
The scope and shadowing of a formal parameter declaration is specified in 6.3 and 6.4.
It is a compile-time error for a lambda expression to declare two formal parameters with the same name.
In Java SE 8, the use of
_
as the name of a lambda parameter was forbidden, and its use discouraged as the name for other kinds of variable (4.12.3). As of Java SE 9,_
is a keyword (3.9) so it cannot be used as a variable name in any context.
It is a compile-time error if a receiver parameter (8.4.1) appears in the FormalParameters of a lambda expression.
This rule is now enforced by the grammar.
It is a compile-time error if the LambdaParameterType of a formal parameter is var
and the VariableDeclaratorId of the same formal parameter has one or more bracket pairs.
It is a compile-time error if a formal parameter that is declared final
is assigned to within the body of the lambda expression.
When the lambda expression is invoked (via a method invocation expression (15.12)), the values of the actual argument expressions initialize newly created parameter variables, each of the declared or inferred type, before execution of the lambda body. The Identifier that appears in the VariableDeclaratorId or the InferredFormalParameterList IdentifierList may be used as a simple name in the lambda body to refer to the formal parameter.
A lambda parameter of type float
always contains an element of the float value set (4.2.3); similarly, a lambda parameter of type double
always contains an element of the double value set. It is not permitted for a lambda parameter of type float
to contain an element of the float-extended-exponent value set that is not also an element of the float value set, nor for a lambda parameter of type double
to contain an element of the double-extended-exponent value set that is not also an element of the double value set.
When the parameter types of a lambda expression are inferred, the same lambda body can be interpreted in different ways, depending on the context in which it appears. Specifically, the types of expressions in the body, the checked exceptions thrown by the body, and the type correctness of code in the body all depend on the parameters' inferred types. This implies that inference of parameter types must occur "before" attempting to type-check the body of the lambda expression.
Copyright © 2018 Oracle America, Inc. 4150 Network Circle, Santa Clara, California 95054, U.S.A. All rights reserved.
The Specification is protected by copyright and the information described therein may be protected by one or more U.S. patents, foreign patents, or pending applications. Except as provided under the following license, no part of the Specification may be reproduced in any form by any means without the prior written authorization of Oracle America, Inc. ("Oracle") and its licensors, if any. Any use of the Specification and the information described therein will be governed by the terms and conditions of this Agreement.
Subject to the terms and conditions of this license, including your compliance with Paragraphs 1 and 2 below, Oracle hereby grants you a fully-paid, non-exclusive, non-transferable, limited license (without the right to sublicense) under Oracle's intellectual property rights to:
Review the Specification for the purposes of evaluation. This includes: (i) developing implementations of the Specification for your internal, non-commercial use; (ii) discussing the Specification with any third party; and (iii) excerpting brief portions of the Specification in oral or written communications which discuss the Specification provided that such excerpts do not in the aggregate constitute a significant portion of the Technology.
Distribute implementations of the Specification to third parties for their testing and evaluation use, provided that any such implementation:
does not modify, subset, superset or otherwise extend the Licensor Name Space, or include any public or protected packages, classes, Java interfaces, fields or methods within the Licensor Name Space other than those required/authorized by the Specification or Specifications being implemented;
is clearly and prominently marked with the word "UNTESTED" or "EARLY ACCESS" or "INCOMPATIBLE" or "UNSTABLE" or "BETA" in any list of available builds and in proximity to every link initiating its download, where the list or link is under Licensee's control; and
includes the following notice: "This is an implementation of an early-draft specification developed under the Java Community Process (JCP) and is made available for testing and evaluation purposes only. The code is not compatible with any specification of the JCP."
The grant set forth above concerning your distribution of implementations of the specification is contingent upon your agreement to terminate development and distribution of your "early draft" implementation as soon as feasible following final completion of the specification. If you fail to do so, the foregoing grant shall be considered null and void.
No provision of this Agreement shall be understood to restrict your ability to make and distribute to third parties applications written to the Specification.
Other than this limited license, you acquire no right, title or interest in or to the Specification or any other Oracle intellectual property, and the Specification may only be used in accordance with the license terms set forth herein. This license will expire on the earlier of: (a) two (2) years from the date of Release listed above; (b) the date on which the final version of the Specification is publicly released; or (c) the date on which the Java Specification Request (JSR) to which the Specification corresponds is withdrawn. In addition, this license will terminate immediately without notice from Oracle if you fail to comply with any provision of this license. Upon termination, you must cease use of or destroy the Specification.
"Licensor Name Space" means the public class or interface declarations whose names begin with "java", "javax", "com.oracle" or their equivalents in any subsequent naming convention adopted by Oracle through the Java Community Process, or any recognized successors or replacements thereof.
No right, title, or interest in or to any trademarks, service marks, or trade names of Oracle or Oracle's licensors is granted hereunder. Oracle, the Oracle logo, Java are trademarks or registered trademarks of Oracle USA, Inc. in the U.S. and other countries.
THE SPECIFICATION IS PROVIDED "AS IS" AND IS EXPERIMENTAL AND MAY CONTAIN DEFECTS OR DEFICIENCIES WHICH CANNOT OR WILL NOT BE CORRECTED BY ORACLE. ORACLE MAKES NO REPRESENTATIONS OR WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT THAT THE CONTENTS OF THE SPECIFICATION ARE SUITABLE FOR ANY PURPOSE OR THAT ANY PRACTICE OR IMPLEMENTATION OF SUCH CONTENTS WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADE SECRETS OR OTHER RIGHTS. This document does not represent any commitment to release or implement any portion of the Specification in any product.
THE SPECIFICATION COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS. CHANGES ARE PERIODICALLY ADDED TO THE INFORMATION THEREIN; THESE CHANGES WILL BE INCORPORATED INTO NEW VERSIONS OF THE SPECIFICATION, IF ANY. ORACLE MAY MAKE IMPROVEMENTS AND/OR CHANGES TO THE PRODUCT(S) AND/OR THE PROGRAM(S) DESCRIBED IN THE SPECIFICATION AT ANY TIME. Any use of such changes in the Specification will be governed by the then-current license for the applicable version of the Specification.
TO THE EXTENT NOT PROHIBITED BY LAW, IN NO EVENT WILL ORACLE OR ITS LICENSORS BE LIABLE FOR ANY DAMAGES, INCLUDING WITHOUT LIMITATION, LOST REVENUE, PROFITS OR DATA, OR FOR SPECIAL, INDIRECT, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF OR RELATED TO ANY FURNISHING, PRACTICING, MODIFYING OR ANY USE OF THE SPECIFICATION, EVEN IF ORACLE AND/OR ITS LICENSORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
You will hold Oracle (and its licensors) harmless from any claims based on your use of the Specification for any purposes other than the limited right of evaluation as described above, and from any claims that later versions or releases of any Specification furnished to you are incompatible with the Specification provided to you under this license.
If this Software is being acquired by or on behalf of the U.S. Government or by a U.S. Government prime contractor or subcontractor (at any tier), then the Government's rights in the Software and accompanying documentation shall be only as set forth in this license; this is in accordance with 48 C.F.R. 227.7201 through 227.7202-4 (for Department of Defense (DoD) acquisitions) and with 48 C.F.R. 2.101 and 12.212 (for non-DoD acquisitions).
You may wish to report any ambiguities, inconsistencies or inaccuracies you may find in connection with your evaluation of the Specification ("Feedback"). To the extent that you provide Oracle with any Feedback, you hereby: (i) agree that such Feedback is provided on a non-proprietary and non-confidential basis, and (ii) grant Oracle a perpetual, non-exclusive, worldwide, fully paid-up, irrevocable license, with the right to sublicense through multiple levels of sublicensees, to incorporate, disclose, and use without limitation the Feedback for any purpose related to the Specification and future versions, implementations, and test suites thereof.
Any action related to this Agreement will be governed by California law and controlling U.S. federal law. The U.N. Convention for the International Sale of Goods and the choice of law rules of any jurisdiction will not apply.
The Specification is subject to U.S. export control laws and may be subject to export or import regulations in other countries. Licensee agrees to comply strictly with all such laws and regulations and acknowledges that it has the responsibility to obtain such licenses to export, re-export or import as may be required after delivery to Licensee.
This Agreement is the parties' entire agreement relating to its subject matter. It supersedes all prior or contemporaneous oral or written communications, proposals, conditions, representations and warranties and prevails over any conflicting or additional terms of any quote, order, acknowledgment, or other communication between the parties relating to its subject matter during the term of this Agreement. No modification to this Agreement will be binding, unless in writing and signed by an authorized representative of each party.