Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala - infer type parameters

Tags:

scala

Why does this code produce an error

def test[A](a: List[A], f: A => A) = a.map(f)

println(test(List(1,2,3), _*2))

error: missing parameter type for expanded function ((x$2) => x$2.$times(2))

shouldn't Scala be able to tell that A is Int?

like image 394
SpiderPig Avatar asked Feb 18 '23 04:02

SpiderPig


1 Answers

You need a second parameter list for this to work. I'm not sure how this is defined in the spec, however I have seen this before.

scala> def test[A](a: List[A])(f: A => A) = a.map(f)
test: [A](a: List[A])(f: (A) => A)List[A]

scala> test(List(1))(_+1)
res1: List[Int] = List(2)
like image 121
Ivan Meredith Avatar answered Feb 27 '23 01:02

Ivan Meredith