Lambda表达式和Java 14中的“? :”运算符

huangapple go评论79阅读模式
英文:

Lambda expresions and "? :" operator in Java 14

问题

各位好,

我有一个关于在lambda表达式中使用`? :`运算符的问题,特别是在switch语句中的使用。您是否可以解释一下,为什么下面的代码不起作用,并且会被标记为`Not a statement`?

代码如下:
```java
switch (separatedTransaction[0]) {
    case "u" -> processUpdate(Integer.parseInt(separatedTransaction[1]), Integer.parseInt(separatedTransaction[2]), separatedTransaction[3]);
    case "o" -> processOrder(separatedTransaction[1], Integer.parseInt(separatedTransaction[2]));
    case "q" -> separatedTransaction.length > 2 ? processQuery(Integer.parseInt(separatedTransaction[2])) : processQuery(separatedTransaction[1]);
    default -> System.out.println("Invalid transaction");
}

而下面的代码则能够正常工作:

switch (separatedTransaction[0]) {
    case "u" -> processUpdate(Integer.parseInt(separatedTransaction[1]), Integer.parseInt(separatedTransaction[2]), separatedTransaction[3]);
    case "o" -> processOrder(separatedTransaction[1], Integer.parseInt(separatedTransaction[2]));
    case "q" -> {
        if (separatedTransaction.length > 2) {
            processQuery(Integer.parseInt(separatedTransaction[2]));
        } else {
            processQuery(separatedTransaction[1]);
        }
    }
    default -> System.out.println("Invalid transaction");
}

在lambda表达式中是否有使用? :运算符的方法呢?如果有的话,能否提供一些代码示例呢?


<details>
<summary>英文:</summary>

Good day to everyone,

I have a question regarding the usage of the `? :` operator in the lambda expressions, especially in the switch statements. Could you kindly clarify, why the below code would not work and will be marked as `Not a statement` 

switch (separatedTransaction[0]) {
case "u" -> processUpdate(Integer.parseInt(separatedTransaction[1]), Integer.parseInt(separatedTransaction[2]), separatedTransaction[3]);
case "o" -> processOrder(separatedTransaction[1], Integer.parseInt(separatedTransaction[2]));
case "q" -> separatedTransaction.length > 2 ? processQuery(Integer.parseInt(separatedTransaction[2])):processQuery(separatedTransaction[1]);
default -> System.out.println("Invalid transaction");
}

And the next one will.

switch (separatedTransaction[0]) {
case "u" -> processUpdate(Integer.parseInt(separatedTransaction[1]), Integer.parseInt(separatedTransaction[2]), separatedTransaction[3]);
case "o" -> processOrder(separatedTransaction[1], Integer.parseInt(separatedTransaction[2]));
case "q" -> {
if (separatedTransaction.length > 2) {
processQuery(Integer.parseInt(separatedTransaction[2]));
} else {
processQuery(separatedTransaction[1]);
}
}
default -> System.out.println("Invalid transaction");
}


Is there a way to use the `? :` operator in the lambda expressions at all?\
If so, could you kindly provide some code examples.

</details>


# 答案1
**得分**: 1

忘记关于那个 switch 语句的一切花哨内容吧,那只是一个误导,与情况完全无关(顺便提一句,那些 `->` 箭头不是 'lambda'。它们只是 switch 作为表达式语法的一部分)。

只需要关注这部分:

`separatedTransaction.length > 2 ? processQuery(Integer.parseInt(separatedTransaction[2])) : processQuery(separatedTransaction[1])`

就能解释这个错误。这是无效的 Java 代码。而且把它放入在 Java 14 中引入的新特性中也不会使它变得合法。

三元操作符的结构如下:

`booleanExpression ? expr1 : expr2`

其中 `expr1` 和 `expr2` 本身必须是表达式。整个表达式的类型取决于 `expr1` 和 `expr2` 的共同类型,关键是这个共同类型不能是 `void`。

问题就在这里:`processQuery` 返回 `void`,因此不能在三元表达式的任何一个“槽”中使用。

<details>
<summary>英文:</summary>

Forget about all the jazz of that switch statement, it&#39;s a red herring; completely irrelevant to the situation (and as a side note, those `-&gt;` arrows aren&#39;t &#39;lambdas&#39;. They&#39;re just part of switch-as-expression syntax).

Just this:

`separatedTransaction.length &gt; 2 ? processQuery(Integer.parseInt(separatedTransaction[2])):processQuery(separatedTransaction[1])`

explains the failure. That is invalid java code. And wrapping it in a new feature introduced in java14 isn&#39;t going to make it any more legal.

The ternary operator construct takes the following form:

`booleanExpression ? expr1 : expr2`

where `expr1` and `expr2` must themselves be expressions. The type of the whole thing is the common ground between the type of expr1 and expr2, and, crucially, that common ground must not be `void`.

That&#39;s the problem: `processQuery` returns `void` and therefore cannot be used in any of the 3 &#39;slots&#39; in a ternary expression.


</details>



huangapple
  • 本文由 发表于 2020年9月22日 21:22:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64010700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定