env:
java:1.8.0_201
scala:2.11.8
IDE: IntelliJ IDEA 2018.3.5 (Ultimate Edition) (Build #IU-183.5912.21)
Intellij Scala plugin:V2018.3.6
I have a class method like this
class ScalaA {
def cal(op: () => String): String = {
op.apply()
}
}
And I want to invoke method cal
, my first try is like this
public class JavaB {
public static void main(String[] args) {
new ScalaA().cal(() -> "Hello");
}
}
But an compile error occured:
Error:(9, 22) java: incompatible types: scala.Function0 is not a functional interface
multiple non-overriding abstract methods found in interface scala.Function0
Then I try to invoke method cal
like this:
public static void main(String[] args) {
new ScalaA().cal(new Function0<String>() {
@Override
public String apply() {
return "aa";
}
});
}
But there is still a compile error
Error:(9, 46) java: is not abstract and does not override abstract method apply$mcV$sp() in scala.Function0
It seems method apply$mcV$sp() doesn't implemented, but I don't see a method named apply$mcV$sp()
in scala.Function0
trait Function0[@specialized(Specializable.Primitives) +R] extends AnyRef { self =>
/** Apply the body of this function to the arguments.
* @return the result of function application.
*/
def apply(): R
override def toString() = "<function0>"
}
, what should I do to solve this problem, could you give me some suggesitions, thanks in advance!