The problem is that the typechecker doesn't know what type to use for List()
. If you specifically annotate the empty list with a type it should work just fine:
val minValue:Int = List.empty[Int] match { // ...
After discussing with @senia I realized you're probably using -1 as a "hack" to say "no minimum" rather than as an actual value. If that's the case, then using the Option
type in Scala might be more clear, as you can return the Some
variant for actual results and the None
variant in the case of an empty list. Scala sequences actually already have a nice method for doing this for you:
scala> List.empty[Int].reduceLeftOption(_ min _)
res0: Option[Int] = None
scala> List(5, 2, 1, 3, 4).reduceLeftOption(_ min _)
res1: Option[Int] = Some(1)