When are braces optional in Java 8 lambda syntax?

前端 未结 4 1993
一向
一向 2020-11-29 10:23

I realise that the Java 8 lambda implementation is subject to change, but in lambda build b39, I\'ve found that braces can only be omitted when the lambda expression returns

相关标签:
4条回答
  • 2020-11-29 10:37

    If there are no braces, the lambda automatically returns the one expression after the -> operator.
    Thus, when you have a lambda that returns nothing, you must use the braces

    0 讨论(0)
  • 2020-11-29 10:56

    This just in: the EG has (mostly) made a decision on syntax.

    After considering a number of alternatives, we decided to essentially adopt the C# syntax. We may still deliberate further on the fine points (e.g., thin arrow vs fat arrow, special nilary form, etc), and have not yet come to a decision on method reference syntax.

    The C# syntax is:

    lambda = ArgList Arrow Body
    ArgList = Identifier
               | "(" Identifier [ "," Identifier ]* ")"
               | "(" Type Identifier [ "," Type Identifier ]* ")"
    Body = Expression
               | "{" [ Statement ";" ]+ "}"
    

    An expression evaluates to something, you can't have void expressions in Java. It is a statement, thus you need {} around it.

    http://mail.openjdk.java.net/pipermail/lambda-dev/2011-September/003936.html

    0 讨论(0)
  • 2020-11-29 10:57

    You may omit the braces when the lambda body is a single expression or a void method invocation. Every expression evaluates to a value, and thus cannot be void.

    If the body of the lambda is a block of statements (e.g. a series of calculations followed by a return statement), or the lambda has no value (i.e. has a void return type) and is not a single void method invocation, you must use the block form, which requires brackets.

    In a block-style lambda, if a value is returned, then all possible code paths must either return a value or throw a Throwable.

    0 讨论(0)
  • 2020-11-29 10:59

    I have tried your code and for the most up-to-date JRE version I think it would be ok.

    The following is what I reference from Oracle Java docs.

    In a lambda expression, you must enclose statements in braces ({}). However, you do not have to enclose a void method invocation in braces.
    For example, the following is a valid lambda expression:

    email -> System.out.println(email)
    

    And the doc explains quite clearly. Hope this can solve your problem.

    References: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

    0 讨论(0)
提交回复
热议问题