英文:
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's a red herring; completely irrelevant to the situation (and as a side note, those `->` arrows aren't 'lambdas'. They're just part of switch-as-expression syntax).
Just this:
`separatedTransaction.length > 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'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's the problem: `processQuery` returns `void` and therefore cannot be used in any of the 3 'slots' in a ternary expression.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论