Alternate Syntax for Zero-Length Parameter List Lambda Expressions

Brian Goetz brian.goetz at oracle.com
Wed Sep 13 14:00:40 UTC 2017


Thanks for your suggestion.  Project Lambda has completed, so this 
mailing list is no longer active.

For the record, I will mention that the question of a further-simplified 
syntax for nilary lambdas was considered at some length by the EG, and 
ultimately rejected as not offering a sufficient return-on-syntax.  In 
the light of experience with Java 8, I think this decision was the 
correct one.


On 9/13/2017 4:24 AM, Janis Schöck wrote:
> 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