为什么引入了`yield`关键字来代替`switch`表达式中的`return`关键字?

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

Why does yield keyword introduced for switch expressions? (not just use return keyword)

问题

当在lambda表达式内部出现返回语句时,它只会导致从lambda表达式中返回,不会导致封闭的方法返回

所以,对于switch表达式,为什么引入了关键字yield而不是使用return关键字呢?第一个return用于从方法返回,而内部的return用于从switch语句返回。

在第一个示例中,return关键字是否令人困惑,而在第二个示例中不令人困惑是否有特殊原因吗?

英文:

> When a return statement occurs within a lambda expression, it simply
> causes a return from the lambda. It does not cause an enclosing method
> to return.

So, for switch expressions why the keyword yield had introduced while return keyword could have been used instead? The first return for returning from method and the inside one for return from switch statement.

class TestClass {
    public int testMethod() {
        // ...
        int a = switch (allOptions) {
            case "case1" -> {
                System.out.println("case 1");
                return 1;
            }
            default -> {
                System.out.println("default");
                return -1;
            }
        };

        return a;
    }
}

Consider this example for lambda expressions:

class TestClass {
    interface FunctionalInterface {
        int test(int n);
    }
    
    public int testMethod() {

        FunctionalInterface f = (n) -> {
            return n * 2;
        };

        return f.test(10);
    }
}

Is there any special reason for that?

Why return keyword is confusing in first example and not confusing in the second one?

答案1

得分: 3

最初,在JEP 325引入了switch表达式时,建议使用break来返回一个值。

> int j = switch (day) {
> case MONDAY -> 0;
> case TUESDAY -> 1;
> default -> {
> int k = day.toString().length();
> int result = f(k);
> break result;
> }
> };

后来在JEP 354中改为使用yield。提到了以下内容:

> 两个语句,break(带标签或不带标签)和yield,有助于轻松区分switch语句和switch表达式:switch语句可以是break语句的目标,但不是switch表达式;而switch表达式可以是yield语句的目标,但不是switch语句。

这可以看作是更改的原因。使用return肯定不会“有助于轻松区分switch语句和switch表达式”,因为您也可以在switch语句中使用return

然后,在JEP 361中进一步解释了重载break会“令人困惑”。

> JEP 325的一个方面是将break语句重载为从switch表达式返回结果值。JDK 12上的反馈表明,这种使用break令人困惑。作为对反馈的回应,JEP 354作为JEP 325的演进被创建。

我想重载return也会令人困惑。

能够从lambda表达式中返回不完全等同于在相同意义上重载return。lambda表达式的主体代表函数体。从函数中使用return是有意义的。而switch表达式的主体并不是一个函数。

英文:

Originally, when switch expressions were introduced in JEP 325, it was proposed that break should be used to yield a value.

> int j = switch (day) {
> case MONDAY -> 0;
> case TUESDAY -> 1;
> default -> {
> int k = day.toString().length();
> int result = f(k);
> break result;
> }
> };

It was changed to yield in JEP 354. It was mentioned that:

> The two statements, break (with or without a label) and yield, facilitate easy disambiguation between switch statements and switch expressions: a switch statement but not a switch expression can be the target of a break statement; and a switch expression but not a switch statement can be the target of a yield statement.

Which could be seen as a reason for the change. Using return for this certainly would not "facilitate easy disambiguation between switch statements and switch expressions", since you can also return in switch statements.

Then, in JEP 361, it was further explained that overloading break was "confusing".

> One aspect of JEP 325 was the overloading of the break statement to return a result value from a switch expression. Feedback on JDK 12 suggested that this use of break was confusing. In response to the feedback, JEP 354 was created as an evolution of JEP 325.

I would imagine overloading return would also be "confusing".

Being able to return from a lambda expression isn't exactly "overloading" return in the same sense. The bodies of lambda expressions represent function bodies. It would make sense for return to return from functions. The body of a switch expression, isn't a function.

答案2

得分: 2

我个人认为在 switch 表达式中使用 yield 关键字很直观。它有助于区分从方法返回值和从 switch 表达式返回值。

因此,这似乎是可读性和架构师选择的问题,而不是功能上的差异。

英文:

Personally, I find it intuitive to use the yield keyword in switch expressions. It helps to distinguish between returning a value from a method and returning a value from a switch expression.

So, it seems to be a matter of readability and the architect's choice, rather than any functional difference.

答案3

得分: 2

> "When a return statement occurs within a lambda expression, it simply causes a return from the lambda. It does not cause an enclosing method to return. ..."

lambda表达式中出现return语句时,它只会导致从lambda中返回。它不会导致封闭的方法返回。

A lambda expression is a closure, so it's actually returning from a method, within that scope.

lambda表达式_是一个闭包_,因此它实际上是从一个方法中返回,位于该作用域内。

> "So, for switch expressions why the keyword yield had introduced while return keyword could have been used instead? ..."

因此,在switch表达式中为什么引入了关键字yield,而不是可以使用return关键字呢?

Using the return keyword here would have overlapped its meaning.

在这里使用_return_关键字会重叠其含义。

It's important to disseminate when you want to simply return from the switch and when you want to return from the enclosing method.

重要的是要区分何时要从_switch_中简单返回,何时要从封闭的方法中返回。

A lot of things in Java are written out more than logically required, purposely.

在_Java_中,许多事情都被有意地写得比逻辑上需要的更多。

An underlying quality of Java is that you can read the code as if it were a sentence—similar to Objective-C.

Java_的一个基本特点是,你可以将代码阅读为一句话,类似于Objective-C_。

英文:

> "When a return statement occurs within a lambda expression, it simply causes a return from the lambda. It does not cause an enclosing method to return. ..."

A lambda expression is a closure, so it's actually returning from a method, within that scope.

> "... So, for switch expressions why the keyword yield had introduced while return keyword could have been used instead? ..."

Using the return keyword here would have overlapped its meaning.

It's important to disseminate when you want to simply return from the switch and when you want to return from the enclosing method.

A lot of things in Java are written out more than logically required, purposely.
An underlying quality of Java is that you can read the code as if it were a sentence—similar to Objective-C.

huangapple
  • 本文由 发表于 2023年7月17日 13:19:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701657.html
匿名

发表评论

匿名网友

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

确定