2

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))

?

dave4420
  • 46,404
  • 6
  • 118
  • 152
Pengin
  • 4,692
  • 6
  • 36
  • 62

1 Answers1

11

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.

Debilski
  • 66,976
  • 12
  • 110
  • 133