question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scala 2 regression: False ambiguous implicit error when supertype companion includes a polymorphic instance

See original GitHub issue

Compiler version

3.2.1 and 3.2.2-RC1

Minimized code

https://scastie.scala-lang.org/rJ1TG8HeSgO8U2Amzt8aYg

trait Supertype {
  def id: Int
}
object Supertype extends SupertypeLowPriorityInstances {
  implicit def supertype: Subtype = Subtype(1)
}
trait SupertypeLowPriorityInstances {
  // deleting this instance fixes the ambiguity error
  implicit def supertypeIfCondition(implicit impossible: Some[Nothing]): Supertype = Subtype(9)
}

final case class Subtype(id: Int) extends Supertype
object Subtype {
  implicit def subtype: Subtype = Subtype(2)
}

object App0 extends App {
  println(implicitly[Subtype])
}

Output

Ambiguous given instances: both method subtype in object Subtype and method supertype in object Supertype match type Subtype of parameter e of method implicitly in object Predef

Expectation

Expected to work, as in Scala 2: https://scastie.scala-lang.org/bwBH5zecS9OWSOwE1bfsJg

In absence of Supertype.supertypeIfCondition there is no ambiguity error, but it’s unclear how could that instance interfere at all, given that Subtype.subtype is a more specific instance than both of the instances in Supertype companion no matter what.

The same bug also affects higher-kinded implicits.

This code is a minimized version of real library code in https://github.com/izumi/izumi which we’re trying to port to Scala 3

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Kordyjancommented, Nov 29, 2022

It is even worse. Any implicit definition in any trait that is a supertype of the Supertype will cause this false error.

trait Unrelated
trait Supertype

object Supertype extends SupertypeLowPriorityInstances {
  implicit def supertype: Subtype = Subtype(1)
}
trait SupertypeLowPriorityInstances {
  implicit def unrelated: Unrelated = ??? // removing this line fixes the snippet
}

final case class Subtype(id: Int) extends Supertype
object Subtype {
  implicit def subtype: Subtype = Subtype(2)
}

object App0 extends App {
  println(implicitly[Subtype])
}

This is not compiling now. What’s even stranger is that this is a consistent behavior since 3.0.0.

0reactions
Kordyjancommented, Dec 7, 2022

A and B are objects, B does not inherit any implicit members from base classes (*), and the companion class of A extends the companion class of B.

I remembered this rule requiring B not to inherit implicit members with the type that we are searching for. Now I see that it literally requires B not to inherit ANY implicit members.

I’ve learned something new.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type Class - Ambiguous Implicit Values Error - Scala Users
Basically, you've set up a situation that is ambiguous: you've got two equally-valid instances of the typeclass for your type. This is a...
Read more >
Ambiguous implicit values for Typeclass - scala - Stack Overflow
As I've already tried to explain in the comments, the problem is that the method override def parse[T](payload: String): Try[T] ...
Read more >
2.10.2-RC1 Milestone · GitHub
DivergentImplicit exception is thrown by c.inferImplicitValue even if silent is set to true has PR. #7166 by scabug was closed on May 11,...
Read more >
Zymposium — Explaining Implicits (Scala 2) - YouTube
... Kit explore the details of implicits. A defining feature of Scala, undergirding many of the language's more advanced features, implicits ...
Read more >
Kotlin Language Documentation 1.7.21
2. Use Kotlin and third-party libraries in your application. Learn more about adding library and tool dependencies to your project.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found