void handling in expression lambdas and lambda conversion
Peter Levart
peter.levart at marand.si
Wed Jan 13 01:24:31 PST 2010
I have two questions about void handling.
Currently, as specified by CfJ 0.6a, expression lambda form takes an expression:
# ( FormalParameters opt ) Expression
Since valid "Expression" as defined by JLS allways denotes a result value, such lambdas can only be assigned to function types that specify a return type that is assignment compatible with the lambda's expression.
My first question is: Would these be considered valid assigments:
#void() lambda1 = #() (Void)null;
#void() lambda2 = #() 1;
The other question is: Would it be possible to relax constraints for the expression specified in expression lambda to allow a method invocation of a void method?
There is already one exception in JLS for "void" expressions - the ExpressionStatement. I think this would be usefull for expression lambdas as well.
So instead of writing:
#() { someMethod(); }
or:
#() ( someMethod(); (Void)null )
One could write:
#() someMethod()
even if someMethod was a void method.
I know that "Method References" are even a more compact syntax, but as specified, are not powerfull enough for specifying this:
int a = 1;
#void(int) lambda = #(int b) method(a, b);
And if "void" expressions were allowed in expression lambdas, other forms of "void" expressions could be introduced that would be valid only in expression lambdas.
Peter
More information about the closures-dev
mailing list