Skip to content

Overridden trait method cannot be called from java #12753

Closed
@felixbr

Description

@felixbr

Compiler version

3.0.0 (it compiles fine with 2.13.6 and earlier)

Minimized code

The following is minimized code from my Scala3 migration attempt of twitter/util.

trait TimeLike[This <: TimeLike[This]]

trait TimeLikeOps[This <: TimeLike[This]] {
  def fromMilliseconds(milliseconds: Long): This

  def fromSeconds(seconds: Long): This = fromMilliseconds(seconds * 1000L)
}

sealed class Duration(val milliseconds: Long) extends TimeLike[Duration]
object Duration extends TimeLikeOps[Duration] {
  def fromMilliseconds(milliseconds: Long): Duration = new Duration(milliseconds)
  
  // note that this override was already a workaround for calling Scala2 from Java. For pure Scala it's not needed.
  override def fromSeconds(seconds: Long): Duration = super.fromSeconds(seconds)
}

Then trying to call the overridden method from Java causes a compile-error:

Duration.fromSeconds(1);

If you want to run the code, here's a runnable branch/commit: felixbr/scala3-example-project@e23ffcf

Output

reference to fromSeconds is ambiguous 
both method fromSeconds(long) in twitter.Duration and method fromSeconds(long) in twitter.Duration match
Duration.fromSeconds

Expectation

It should compile as it did in 2.13.6 and earlier.

Cheers
~ Felix

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions