scala 如何在scala中定义一个函数不返回或返回void
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32018257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define a function does not return or return void in scala
提问by Kuan
With more and more confused raised up, I even find that I can not understand simple Function definition in Scala:
随着越来越多的困惑被提出,我什至发现我无法理解Scala中简单的Function定义:
If I just want to define a function doing nothing but some unuseful addition, how can I do that like this:
如果我只想定义一个函数,除了一些无用的添加之外什么都不做,我怎么能这样做:
def nothing(a:Int, b:Int) = {
a = a+1; b=b+1;
}
What I want is add 1 to a and b and return nothing or void.
我想要的是将 1 添加到 a 和 b 并且不返回任何内容或无效。
I kinda feel the more I learnt, the more I begin unfamiliar with, even to those I learnt before, this is terrible.
我有点觉得我学得越多,我就越不熟悉,即使是我以前学过的人,这也太可怕了。
回答by om-nom-nom
Your question boils down to two parts:
你的问题归结为两部分:
1) How do I define function which returns nothing? Pretty easy, just encode it to type signature, Unit is the scala way to say void (they're the same actually, java function that returns void will return Unit in scala world):
1)如何定义不返回任何内容的函数?很简单,只需将其编码为类型签名,Unit 是表示 void 的 Scala 方式(实际上它们是相同的,返回 void 的 java 函数将在 Scala 世界中返回 Unit):
scala> def foo(x: Int): Unit = x * x
foo: (x: Int)Unit
scala> foo(1)
scala>
Previously scala encouraged so called proceduredefinition (see, no equals sign):
以前 Scala 鼓励所谓的过程定义(见,没有等号):
scala> def foo(x: Int) { x * x }
foo: (x: Int)Unit
scala> foo(2)
scala>
But it's error prone and thus discouraged.
但它容易出错,因此不鼓励。
2) How do I alter primitive that was passed into the function? You can't, scala, as well as Java does not allow this and it's for a great good.
2) 如何更改传递给函数的原语?你不能,scala,以及 Java 不允许这样做,这是一个很好的好处。
回答by Bacon
As Chris and om-nom said, you cannot alter primitive passed as arguments to a function, and will need to declare new vals. Also, you can specify the return type of your variable by appending : Unit(or any valid type) after the parameters, in the signature, as following:
正如 Chris 和 om-nom 所说,你不能改变作为参数传递给函数的原语,并且需要声明新的 val。此外,您可以通过: Unit在签名中的参数后附加(或任何有效类型)来指定变量的返回类型,如下所示:
def nothing(a:Int, b:Int): Unit = { val new_a = a+1; val new_b = b+1 }
Note that the implicit type of your function was already Unit, i.e void in scala.
请注意,您的函数的隐式类型已经是Unit,即 scala 中的 void。
回答by Wilson de Carvalho
If you just came from C++, Unit is the keyword your looking for. It works exactly as void regarding function return.
如果你刚从 C++ 过来,Unit 就是你要找的关键词。关于函数返回,它的工作原理与 void 完全一样。
It seems to me the issue here is that still don't understand functional programming. It is not possible to change value of passed parameters:
在我看来,这里的问题是仍然不了解函数式编程。无法更改传递参数的值:
def nothing(a:Int, b:Int) = {
a = a+1; b=b+1; // this won't work
}
Instead, you should return new values with the result operation. In this case, you should sum 1 to a and b and return both using a Tuple2. You can do this using the following sintaxes:
相反,您应该使用结果操作返回新值。在这种情况下,您应该将 1 与 a 和 b 相加,并使用 Tuple2 返回两者。您可以使用以下语法来做到这一点:
def nothing(a:Int, b:Int) = {
(a + 1, b + 1)
}
or
或者
def nothing(a:Int, b:Int) = {
Tuple2(a + 1, b + 1)
}
or
或者
def nothing(a:Int, b:Int) = {
Tuple2[Int, Int](a + 1, b + 1)
}

