scala.util.Try递归函数引发编译错误
给定以下返回Try[Int]的递归函数,我得到一个编译错误scala.util.Try递归函数引发编译错误,scala,Scala,给定以下返回Try[Int]的递归函数,我得到一个编译错误 类型不匹配;找到:scala.util.Try[Int]必需:Int 但是函数返回Try[Int],有什么问题吗?如果Try导致失败,我需要函数抛出一个错误 def getInt(i: Int): Try[Int] = Try { if (i == 0) i else { val j = i - 1 getI
类型不匹配;找到:scala.util.Try[Int]必需:Int
但是函数返回Try[Int]
,有什么问题吗?如果Try导致失败,我需要函数抛出一个错误
def getInt(i: Int): Try[Int] = Try {
if (i == 0)
i
else {
val j = i - 1
getInt(j) // <-- error is thrown in this line
}
}
def getInt(i:Int):Try[Int]=Try{
如果(i==0)
我
否则{
valj=i-1
getInt(j)//您现在可以在Try中进行尝试
试试(哈!)这个:
你现在可以试一下了
试试(哈!)这个:
方法getInt
returnTry[Int]
因此您是这样编写的:
Try { if (i == 0) return integer else return Try[Int] }
要解决此问题,您必须这样做:
def getInt(i: Int): Try[Int] =
if (i == 0)
Success(i)
else {
val j = i - 1
getInt(j) // <-- error is thrown in this line
}
def getInt(i:Int):尝试[Int]=
如果(i==0)
成功(一)
否则{
valj=i-1
getInt(j)/methodgetInt
returnTry[Int]
这样写:
Try { if (i == 0) return integer else return Try[Int] }
要解决此问题,您必须这样做:
def getInt(i: Int): Try[Int] =
if (i == 0)
Success(i)
else {
val j = i - 1
getInt(j) // <-- error is thrown in this line
}
def getInt(i:Int):尝试[Int]=
如果(i==0)
成功(一)
否则{
valj=i-1
getInt(j)//