scala/scala3

Could not inherit Scala class in Java where declaration-site variance type used for covariant return type

Opened this issue · 0 comments

Compiler version

3.6.3-RC2

Minimized code

trait I0[+T] {
  def func(): T
}
abstract class A {
  def func(): I0[Any]
}
class A1 extends A {
  final override def func(): I0[String] = ???
}

Java class:

public class A2 extends A1 {
}

Output

B.java:1: error: func() in A2 cannot override func() in A1
public class B extends A2 {}
       ^
  return type A0<String> is not compatible with A0<Object>
1 error

Expectation

Compiled or use something like @JvmWildCard in Kotlin to make it can compile in Java.

It may be difficult to directly compile the above code, but it is possible to introduce annotations like @JvmWildCard like Kotlin. See KT-11552