Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala map of function using "_"

Tags:

scala

Why is it that (in the Scala REPL) I can write, for example,

def double(d: Int) = 2*d
(0 until 10).zipWithIndex.map(i => double(i._1))

or just

(0 until 10).zipWithIndex.map(_._1)

yet I can't write

(0 until 10).zipWithIndex.map(double(_._1))
error: missing parameter type for expanded function ((x$1) => x$1._1) (0 until 10).zipWithIndex.map(double(_._1))

?

like image 942
Pengin Avatar asked Sep 17 '26 08:09

Pengin


1 Answers

Scala tries to expand _._1 inside double. So, it thinks you want to have

(0 until 10).zipWithIndex.map(double(i => i._1))

However, it also sees that i => i._1 does not really fit into one of double’s argument types, so it complains and asks you to give a type hint to help the compiler. In this case, though, there cannot be a correct type definition, so the error message is kind of wrong there.

like image 95
Debilski Avatar answered Sep 19 '26 04:09

Debilski