4

使用 scala wart我得到:

  def lastWithRecursion(input: Seq[Int]): Try[Int] = input match {
      case head :: Nil => Success(head)
      case _ :: tail => lastWithRecursion(tail)
      case _ => Failure(new NoSuchElementException("No such element")) // how to avoid inferred type containing nothing.
  }

如何避免inferred type containing nothing

4

1 回答 1

6

尝试添加通用到Failure

def lastWithRecursion(input: Seq[Int]): Try[Int] = input match {
    case head :: Nil => Success(head)
    case _ :: tail => lastWithRecursion(tail)
    case _ => Failure[Int](new NoSuchElementException("No such element")) // how to avoid inferred type containing nothing.
}
于 2015-11-27T08:20:58.650 回答