为什么 Scala 需要递归函数的返回类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3739133/
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
Why does Scala require a return type for recursive functions?
提问by Joshua
In the code snippet included below, I have a recursive function call, used to facilitate a retry if a network call fails (Amazon SimpleDB will occasionally return a 503 and require retry.)
在下面包含的代码片段中,我有一个递归函数调用,用于在网络调用失败时促进重试(Amazon SimpleDB 偶尔会返回 503 并要求重试。)
When I try to compile, the Scala complains recursive method simpledb_update needs result type.
当我尝试编译时,Scala 会抱怨recursive method simpledb_update needs result type.
// sends data to SimpleDB. Retries if necessary
def simpledb_update(name: String, metadata: Map[String,String], attempt: Int) = {
try {
db(config("simpledb_db")) += (name, metadata)
} catch {
case e =>
// if it fails, try again up to 5 times
if(attempt < 6)
{
Thread.sleep(500)
simpledb_update(name, metadata, attempt + 1)
} else
AUlog(name + ": SimpleDB Failed")
}
}
Why is this required on recursive functions? My thought is to just return a true/false boolean to satisfy the compiler... the following compiles fine.
为什么递归函数需要这样做?我的想法是只返回一个真/假布尔值以满足编译器......以下编译正常。
// sends data to SimpleDB. Retries if necessary
def simpledb_update(name: String, metadata: Map[String,String], attempt: Int): Boolean = {
try {
db(config("simpledb_db")) += (name, metadata)
true
} catch {
case e =>
// if it fails, try again up to 5 times
if(attempt < 6)
{
Thread.sleep(500)
simpledb_update(name, metadata, attempt + 1)
} else
AUlog(name + ": SimpleDB Failed")
false
}
}
采纳答案by Dave Griffith
As I understand it, recursive functions need a return type because the type inference algorithm is not powerful enough to determine return types for all recursive functions.
据我了解,递归函数需要返回类型,因为类型推断算法不够强大,无法确定所有递归函数的返回类型。
However, you don't need to make up a return type, you just need to declare the return type you were already using: Unit. Unit is a special type with only one element (). It's also the type of most "statements" in Scala, and is the return type to declare for methods that don't need to return anything, but are executed only for their side-effects (as yours is). You can either declare your method as returning unit as you would other types
但是,您不需要组成返回类型,您只需要声明您已经使用的返回类型:Unit。Unit 是一种特殊类型,只有一个元素 ()。它也是 Scala 中大多数“语句”的类型,并且是为不需要返回任何内容但只为它们的副作用(如您的)执行的方法声明的返回类型。您可以像其他类型一样将方法声明为返回单元
def simpledb_update(name: String, metadata: Map[String,String], attempt: Int):Unit = {
More idiomatically Scala provides a special syntax for Unit-returning methods, just leave off the return type and the equals sign
更惯用地,Scala 为返回单元的方法提供了一种特殊的语法,只需去掉返回类型和等号
def simpledb_update(name: String, metadata: Map[String,String], attempt: Int){
According to scala style guide you should prefer to use equal sign
根据 Scala 风格指南,您应该更喜欢使用等号
回答by Thomas
Just remove the = from the line and it will return Unit, this means that you don't need to return anything.
只需从行中删除 = 即可返回 Unit,这意味着您不需要返回任何内容。
def simpledb_update(name: String, metadata: Map[String,String], attempt: Int) {
I believe the need for return types, it to make sure all recursion paths have the correct type. On a normal function, the type would be inferred from all return points.
我相信需要返回类型,以确保所有递归路径都具有正确的类型。在普通函数中,类型将从所有返回点推断出来。

