Really Provisional Syntax for Lambda Project: October 2010
Maurizio Cimadamore
This document is a rather informal and basic guide on the syntax currently implemented in the lambda prototype.
1. Lambda expressions
---------------------
Lambda expressions starts with the special '#' character.
#() { ... }
# { ... }
Note: a nil-ary argument list can be omitted.
1.1 Formal parameters
The formal parameters accepted by a lambda expression are enclosed in the lambda block. The special arrow symbol '->' is used to separate the lambda formal parameter list from the body of the lambda expression.
# { int x, int y -> ... }
# { x, y -> ... }
# { -> ... } //explicit alternative for nilary syntax (shown above)
Note: the type of a lambda parameter can be omitted - in this case the compiler will infer a type from the target type of the lambda conversion.
1.2 Body
The body of a lambda expression can be either (i) an expression or (ii) a statement list (terminated by a ';').
# { System.out.println("Hello"); System.out.println("Lambda!"); } //statement-body - implicit nilary lambda
# { int x, int y -> x + y } //expression-body - explicit 2-ary lambda
1.3 Target-type
Special syntax for target-type is not available anymore. Instead, explicit target-type must be specified using a regular cast:
Mapper m = (Mapper)#(x){ x.toString(); }
Note: the above cast can be handy in case of overload resolution.
2. Exception transparency
-------------------------
Exception type-parameters are declared using the 'throws' keyword:
class Foo { ... }
2.1 Disjunctive Types
An exception type-parameter can be instantiated by a disjunctive type, a list of reference types separated by a '|' char:
Foo
The nil-ary disjunctive is denoted using the keyword 'void':
Foo
Note: 'void' is allowed as an actual type-argument only in correspondence with exception type-parameters.
3. Defender Methods
-------------------
Defender methods are declared using the 'extension' keyword, as follows:
extension List map(Mapper r) default Collections.listMapper;
A defender method declaration is structured in two parts, (i) a method signature, declaring formal arguments, formal type-arguments and return-type and (ii) a default implementation, that is, the implementation the method should default to if none is found in the class implementing the extended interface.
4. Method References
--------------------
The syntax for method references uses the infix '#' operator; the selector of a method reference can be either a valid Java expression or a type:
Foo#bar()
new Foo()#bar()
4.1. Actual Arguments
The actual argument type list associated with a method reference is optional:
Foo#baz
However, if required, actual argument types can be specified after the method references, enclosed in parenthesis, as in:
Foo#baz(Integer, String).
Note: the explicit syntax can be handy in case of overload resolution.