scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Typeclass extension methods do not see instances put in typeclass companion object

Open goshacodes opened this issue 6 months ago • 0 comments

Hello, not sure if this is a bug. Extension method =:= do not see typeclass instances put in typeclass companion object, it feels not very clear why should I use import in this case and how this import fixes search of extension method If I put instance in concrete type companion object syntax works without import

Compiler version

3.3.3 3.5.0

Minimized code

trait Eq[A]:
  def eqv(first: A, second: A): Boolean

  extension (first: A)
    def =:=(second: A): Boolean = eqv(first, second)

object Eq:
  given Eq[String] = (_, _) => true

case class User(str: String) 

object User:
  given Eq[User] = (_, _) => true

User("") =:= User("") // compiles
"" =:= "" // not compiles without import Eq.given

Output

value =:= is not a member of String, but could be made available as an extension method.

The following import might fix the problem:

  import Playground.Eq.given_Eq_String

Expectation

Compiles without import Eq.given

goshacodes avatar Aug 28 '24 13:08 goshacodes