Scala 2 regression: False ambiguous implicit error when supertype companion includes a polymorphic instance
See original GitHub issueCompiler 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:
- Created 10 months ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It is even worse. Any implicit definition in any trait that is a supertype of the
Supertype
will cause this false error.This is not compiling now. What’s even stranger is that this is a consistent behavior since 3.0.0.
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.