Skip to content

Unexpected compilation error while calling Scala from Java #12556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
anthony-cros opened this issue Mar 14, 2022 · 4 comments
Closed

Unexpected compilation error while calling Scala from Java #12556

anthony-cros opened this issue Mar 14, 2022 · 4 comments

Comments

@anthony-cros
Copy link

Reproduction steps

Scala version: 2.13.8 (also in 2.12.x)

// $ cat Bar.scala:

trait Bar[F] {
          def f  : F
    final def bar: F = f // the real life example is more involved here
}

// ==================================================
//$ cat Foo.scala:

class Foo() extends Bar[Foo] {
    override def f: Foo = this
}


// ==================================================
//$ cat Baz.java:

class Baz {
    public static void main(String args[]) {
    //  Foo foo =       new Foo().bar();  // compilation error: incompatible types: Object cannot be converted to Foo
        Foo foo = (Foo)(new Foo().bar()); // compiles
    }
}

Problem

See more details on Scala Users: https://users.scala-lang.org/t/issue-with-calling-scala-code-from-java/8321

@jxnu-liguobin
Copy link
Member

java15 and scala2.13.x also find error.
And it's interesting, it has been converted implicitly in scala.

object Test {
  // $ cat Bar.scala:

  trait Bar[F] {
    def f  : F
    final def bar: F = f // the real life example is more involved here
  }

  // ==================================================
  //$ cat Foo.scala:

  class Foo() extends Bar[Foo] {
    override def f: Foo = this
  }

  val useInScala =  new Foo().bar
}

show-trees, There have a asInstanceOf for converting as Foo type

    <stable> <accessor> def useInScala(): sandbox.Test.Foo = Test.this.useInScala;
    def <init>(): sandbox.Test.type = {
      Test.super.<init>();
      Test.this.useInScala = new sandbox.Test.Foo().bar().$asInstanceOf[sandbox.Test.Foo]();
      ()
    }

@Jasper-M
Copy link

I think this is #8905.

@jxnu-liguobin
Copy link
Member

I think this is #8905.

So it seems impossible to solve?

@SethTisue
Copy link
Member

Closing as duplicate. If someone is interested in reviving efforts to address #8905, let's discuss that there rather than here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants