I have been learning Scala these days, and today I ran into some issue that I cannot understand.
Suppose we have the following parametric function definition:
def filter[T](source: List[T], predicate: T=>Boolean): List[T] = {
source match {
case Nil => Nil
case x::xs => if(predicate(x)) x::filter(xs, predicate)
else filter(xs, predicate)
}
}
Now, this works just fine if I invoke it as follows:
filter(List(1,2,3,4,5,6), ( (n:Int) => n % 2 == 0))
But if remove the type tag, it appears Scala cannot infer that the type of T is Int.
filter(List(1,2,3,4,5,6), ( n => n % 2 == 0))
So, I am forced to provide explicit type information in this call.
Does anybody know why Scala is not capable of inferring the type of T in this call. The list is evidently a List of Ints, I cannot see why it cannot infer that the type of n is also Int.