Alternate Syntax for Zero-Length Parameter List Lambda Expressions
Janis Schöck
janis.schoeck at stl-software.de
Wed Sep 13 08:24:45 UTC 2017
Dear Lambda-Expression EG Team,
Having worked with lambda expressions for only a short time now, I have noticed,
that both the Runnable and Supplier Interfaces are very common candidates for lambda expressions.
If they are passed as sole parameters in a parameter list, they tend to look like this:
someObject.setSomeProvider(() -> doSomething(a));
someHandler.setTrigger(() -> doSomethingElse(b));
The resulting examples are trivial, but nevertheless seem a bit cluttered, considering that they effectively feature
a function pointer which could usually be written with the :: operator, had they not parameters induced from the surrounding scope.
There have to be 4 (to 5 if you count the space) characters to express what could be considered a single operator.
To simplify both writing and optimize readability, I propose the following operator as an optional alternative to () ->
+>
This operator implies a 'strikethrough lambda arrow', which suggests that nothing is passed into the method, which
perfectly mirrors the actual behavior. The + character is also easy to type and hard to mistake for some other character.
In conjunction with the single-parameter lambda expression, which also optionally reduces the parenthesis, I believe
this operator complements the simplicity approach.
Here are a bunch of examples for the operator:
Supplier<Object> supplier = () -> myObj.get(context);
Supplier<Object> supplier = +> myObj.get(context);
myMethod(() -> myObj.get(context));
myMethod(+> myObj.get(context));
myMethod(paramA, paramB, () -> myObj.get(context));
myMethod(paramA, paramB, +> myObj.get(context));
myMethod(() -> myObj.get(a()), () -> myOtherObj.get(b()), () -> myThirdObj.get(c()));
myMethod(+> myObj.get(a()), +> myOtherObj.get(b()), +> myThirdObj.get(c()));
The operator also seems syntactically sound, as to my knowledge there is only one such combination of characters in any
preexisting java syntax (a++>b and derivatives like a++>=b) but since the same is true for (a-->b)I suppose this
issue is solved. From a compiler approach, the change should prove trivial, as the token could theoretically be statically
replaced by () -> (excepting ++>) and should therefore barely impact the syntax tree.
Of course any other combination of characters could also be used, but +> seemed logical.
Also considered, but (for various reasons) discarded where:
:>
#>
~>
!>
=>
Thank you for both your time, and a good job with a great to use syntax adaption!
Sincerely,
Janis Schöck
More information about the lambda-spec-comments
mailing list