3

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!

刘思凡
  • 423
  • 2
  • 14
  • I am not familiar with scala and functional arguments, but I wish all questions on SO could be written in such a high quality. This is understandable, it has all the codes to reproduce. It is well written. Thumbs up. – Seelenvirtuose Apr 10 '19 at 07:44
  • @DmytroMitin I might be wrong ... but it seems your linked question asks the other way round: How to provide a Scala function for a Java method that expects an instance of a functional interface as an argument. – Seelenvirtuose Apr 10 '19 at 07:47
  • 1
    @Seelenvirtuose The question contains answers for both directions: Java to Scala and Scala to Java. – Dmytro Mitin Apr 10 '19 at 07:49
  • @刘思凡 I can't reproduce your error. With Scala 2.12.8 + JDK 1.8.0_162 `ScalaA` and `JavaB` compile without errors. – Dmytro Mitin Apr 10 '19 at 07:51
  • @刘思凡 What are your versions? – Dmytro Mitin Apr 10 '19 at 07:51
  • Ok, it seems you use 2.11. – Dmytro Mitin Apr 10 '19 at 07:53
  • @DmytroMitin My java version is 1.8.0_201 and scala version is 2.11.8, I'll add them to the question later. – 刘思凡 Apr 10 '19 at 07:55
  • 3
    @刘思凡 Either use 2.12 or in 2.11 use `scala.compat.java8.JFunction.func` from `scala-java8-compat`. Deatils are here https://stackoverflow.com/a/47381315/5249621 – Dmytro Mitin Apr 10 '19 at 07:58
  • It's useful and make sense,thank you very much! – 刘思凡 Apr 10 '19 at 08:11
  • @刘思凡 Regarding your second approach, you can't see `apply$mcV$sp()` because this method is auto-generated by Scala compiler (as most of methods with dollar sign in their names). Method `apply$mcV$sp()` is for primitives and generated because of annotation `@specialized` in `Function0`. The following code compiles: https://gist.github.com/DmytroMitin/d8ac6103c6339091ebd8e11446f4077d – Dmytro Mitin Apr 10 '19 at 08:24
  • 1
    @Seelenvirtuose Thank you for your encouragement! But maybe I should do more search before asking a question. – 刘思凡 Apr 10 '19 at 11:10

0 Answers0