Skip to content

Ambiguous implicit error involving implicits defined in the companions of classes where one is a subclass of another (works in Scala 2) #12125

@dlwh

Description

@dlwh

or at least, it's changed from Scala 2.

First I want to say that Scala 3 is turning out to be a very nice language and I’m excited for what we can do with it. Thank you.

I am in the process of trying to get Breeze compiling under Scala 3. There is a common pattern in Breeze in how I organize implicits, where for a class XVector I have a trait XVectorOps with a bunch of implicits in it that object XVector extends. Usually these are stacked a bit. In Scala 2 this all worked fine, but in Scala 3, this leads to problems in the case where there is a subtype relationship between different XVectors.

Compiler version

3.0.0-RC2

Minimized code

This is a minimized example that compiles fine in Scala 2 but does not work in Scala 3:

trait Op[T, U]

trait HasOps[+This] {
  def foo[TT>:This, U](u: U)(implicit op: Op[TT, U]) = op
}

trait Vector extends HasOps[Vector]

class DenseVector extends Vector with HasOps[DenseVector]

trait VectorOps {
  implicit def vOp: Op[Vector, Double] = ???
}

object Vector extends VectorOps

trait DenseVectorOps {
  implicit def dvOp: Op[DenseVector, Double] = ???
}

object DenseVector extends DenseVectorOps

object Foo {
  def bar() = {
    val dv = new DenseVector
    dv.foo(3.0)
  }
}

Output

26 |    dv.foo(3.0)
   |               ^
   |ambiguous implicit arguments: both method dvOp in trait DenseVectorOps and method vOp in trait VectorOps match type Op[TT, Double] of parameter op of method foo in trait HasOps

Expectation

This compiles fine in Scala 2, and if I remove the XVectorOps classes and inline them into the companions directly this compiles fine in Scala 3, but the inheritance trips it up.

Is this intentional?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions