Skip to content

Implicit priority in companions of the ancestors of a type #10427

@julienrf

Description

@julienrf

Consider the following cocde:

object Priority {

  class Ev[-A, +B]

  class Foo[A]

  class Quux

  // Foo is associated with Bar
  // Quux is part of Foo
  class Bar extends Foo[Quux]

  object Foo {
    implicit val ev: Ev[Any, Boolean] = new Ev
  }

  object Bar {
    implicit val ev: Ev[Any, String] = new Ev
  }

  object Quux {
    implicit val ev: Ev[Any, Int] = new Ev
  }

  // Here we require an implicit parameter of type `Ev[Bar, ?]`.
  // Candidates will be looked up from the companion objects of the
  // types associated with `Bar`: `Bar`, `Foo` and `Quux`
  def usage[X](implicit ev: Ev[Bar, X]): Unit = ()

  usage
}

We get ambiguous candidates: Quux.ev and Bar.ev. Note that Foo.ev has a lower priority and does not cause ambiguity. I would expect Quux.ev to also have a lower priority because it is defined in the companion of a type (Quux) which is part of a type (Foo[Quux]) which is associated with Bar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions