how to avoid inferred type containing nothing with scala wart?

2k views Asked by At

using scala wart I get:

  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.
  }

how to avoid inferred type containing nothing?

1

There are 1 answers

0
Reactormonk On BEST ANSWER

Try with added generic to 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.
}