在 Scala 中返回 [英] Return in Scala

查看:56
本文介绍了在 Scala 中返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名 Scala 程序员新手,遇到了一个奇怪的行为.

I am a newbie scala programmer and came across a weird behavior.

def balanceMain(elem: List[Char]): Boolean =
  {
    if (elem.isEmpty)
      if (count == 0)
        true;
      else false;

    if (elem.head == '(')
      balanceMain(elem.tail, open, count + 1);....

上面基本上我想如果 elem.isEmptycount == 0 返回 true.否则,我想返回false.

Above basically I want to return true if elem.isEmpty and count == 0. Otherwise, I want to return false.

现在上面我已经读到不需要在scala中添加return语句.所以我省略了上面的 return .但它不返回布尔值.如果我添加一个 return 语句作为 return true.它完美地工作.为什么会这样?

Now above I have read that there is no need to add a return statement in scala. So I have omitted return above. But it doesn't return the boolean. If I add a return statement as return true. it works perfectly. Why is it so?

另外,为什么在 Scala 中使用 return 语句被认为是一种不好的做法

Also, why is it considered a bad practice to have return statements in scala

推荐答案

这并不像省略 return 关键字那么简单.在 Scala 中,如果没有 return 则最后一个表达式被视为返回值.因此,如果最后一个表达式是您想要返回的内容,那么您可以省略 return 关键字.但是如果你想要返回的不是最后一个表达式,那么Scala不会知道你想要返回它.

It's not as simple as just omitting the return keyword. In Scala, if there is no return then the last expression is taken to be the return value. So, if the last expression is what you want to return, then you can omit the return keyword. But if what you want to return is not the last expression, then Scala will not know that you wanted to return it.

示例:

def f() = {
  if (something)
    "A"
  else
    "B"
}

这里函数 f 的最后一个表达式是一个 if/else 表达式,它的计算结果是一个字符串.由于没有显式的 return 标记,Scala 会推断你想要返回这个 if/else 表达式的结果:一个字符串.

Here the last expression of the function f is an if/else expression that evaluates to a String. Since there is no explicit return marked, Scala will infer that you wanted to return the result of this if/else expression: a String.

现在,如果我们在 if/else 表达式之后添加一些东西:

Now, if we add something after the if/else expression:

def f() = {
  if (something)
    "A"
  else
    "B"

  if (somethingElse)
    1
  else
    2
}

现在最后一个表达式是一个计算结果为 Int 的 if/else 表达式.所以 f 的返回类型将是 Int.如果我们真的想让它返回字符串,那么我们就有麻烦了,因为 Scala 不知道这就是我们的意图.因此,我们必须通过将字符串存储到变量并在第二个 if/else 表达式之后返回它来修复它,或者通过更改顺序使字符串部分最后发生.

Now the last expression is an if/else expression that evaluates to an Int. So the return type of f will be Int. If we really wanted it to return the String, then we're in trouble because Scala has no idea that that's what we intended. Thus, we have to fix it by either storing the String to a variable and returning it after the second if/else expression, or by changing the order so that the String part happens last.

最后,即使使用像您这样的嵌套 if-else 表达式,我们也可以避免使用 return 关键字:

Finally, we can avoid the return keyword even with a nested if-else expression like yours:

def f() = {
  if(somethingFirst) {
    if (something)      // Last expression of `if` returns a String
     "A"
    else
     "B"
  }
  else {
    if (somethingElse)
      1
    else
      2

    "C"                // Last expression of `else` returns a String
  }

}

这篇关于在 Scala 中返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆