Darin
Darin

Reputation: 143

Scala underscore: error: missing parameter type for expanded function

I recently started learning scala and I was a bit confused by how underscore works. While it in most case gives you convenient anonymous method, sometimes it just confuses compiler (and me).

For example,

This works

val randomList = List(1,2,3)
randomList.groupBy(x => x)

and this

val randomList = List(1,2,3)
randomList.groupBy(_ + 1)

but not this

val randomList = List(1,2,3)
randomList.groupBy(_)

error: missing parameter type for expanded function ((x$1) =>  randomList.groupBy(x$1))
          randomList.groupBy(_)

A few similar cases are asked in the forum (i.e this one), most of them say this is expanded to an anonymous function like this x => randomList.groupBy(x). I just do not know why the complier knows how to compiler second case but not the third. Also, is there a way to resolve the last case without writing a explicit function?

Upvotes: 2

Views: 2293

Answers (0)

Related Questions