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

One problem that I constantly encounter in scala with lambda expressions. For instance,

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

gives me an error, for example:

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

But if I do the extension myself:

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

It works great.

Is this a scala bug? Or am I doing something terribly wrong?

+6
source share
1 answer

Using placeholder syntax for anonymous functions is limited to expressions . In code, you are trying to use a wildcard in an assignment statement that does not match the expression.

If you look closely at the error, you will see that the expression on the right side of your destination is what expands into an anonymous function.

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

 userJars = userJars ++ JarBuilder.findContainingJar(clazz) 
+6
source

Source: https://habr.com/ru/post/905052/


All Articles