Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Missing parameter type for expanded function" when using _ (underscore)?

One problem I continually run into scala is with lambda' expressions. For instance

JarBuilder.findContainingJar(clazz).foreach {userJars = userJars + _ }

gives me an error like:

missing parameter type for expanded function ((x$1) => userJars.$plus(x$1))

Yet if I do the expansion myself:

JarBuilder.findContainingJar(clazz).foreach {x => userJars = userJars + x }

It works fine.

Is this a Scala bug? Or am I doing something horribly wrong?

like image 524
Heptic Avatar asked Jan 04 '12 05:01

Heptic


1 Answers

The usage of placeholder syntax for anonymous functions is restricted to expressions. In your code you are attempting to use the wildcard in an assignment statement which is not the same as an expression.

If you look closely at the error, you can see that the expression on the right hand side of your assignment is what is being expanded into an anonymous function.

Given what you are trying to accomplish however you may want to consider the following

userJars = userJars ++ JarBuilder.findContainingJar(clazz)
like image 122
Neil Essy Avatar answered Nov 02 '22 10:11

Neil Essy