Skip to content

implicit object is not available for implicit resolution #12440

Closed
@myazinn

Description

@myazinn

reproduction steps

using Scala 2.13.6,

trait Companion[T]

abstract class Service[T: Companion]

object Foo {
  // can't find required implicit
  class Impl extends Service[String]

  implicit object Impl extends Companion[String]
}

problem

I'm expecting the above code to compile but it doesn't. If I (1) add dummy line that calls for this implicit value too, or (2) change the definition order, or (3) change implicit object to implicit lazy val then the code compiles fine
1.

trait Companion[T]

abstract class Service[T: Companion]

object Foo {
  def unused: Any = implicitly[Impl.type]
  
  class Impl extends Service[String]

  implicit object Impl extends Companion[String]
}
trait Companion[T]

abstract class Service[T: Companion]

object Foo {
  implicit object Impl extends Companion[String]
  
  class Impl extends Service[String]
}
trait Companion[T]

abstract class Service[T: Companion]

object Foo {  
  class Impl extends Service[String]

  implicit lazy val Impl: Companion[String] = ???
}

I'm really surprized that (1) fixes this issue. Intuitively it should not affect implicit resolution of existing code at all.

Initially I've faced this issue on Scala 2.12.14 which has exactly the same behavior in this case. Also, if I compile this code in Scala 3 then it works fine. Maybe you will find it useful.

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