-
Notifications
You must be signed in to change notification settings - Fork 1.1k
ambiguous implicit object arguments for contravariant trait #14169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This doesn't look related to Scala.js per se. It looks like a typer thing. I can reproduce the issue using Scala/JVM: trait Render[-T] { def render(value: T): String }
implicit object StringRender extends Render[String]:
def render(value: String): String = value
end StringRender
implicit object UndefOrRender extends Render[scala.|[String, Unit]] {
def render(value: scala.|[String, Unit]): String =
if value == () then "empty" else value.asInstanceOf[String]
}
implicitly[Render[String]]
// ambiguous implicit arguments: both object StringRender in object Playground and object UndefOrRender in object Playground match type Playground.Render[String] of parameter e of method implicitly in object Predef |
Seems nobody wants to take this on. I don't know enough about scala.I to figure out what goes on here. My guess is that it's the different handling of contravariant parameters, which would mean that Scala 3's behavior is as intended. |
I think it's just that neither object type is more specific than the other, if you use implicit vals or givens then it works out since we end up comparing the types trait Render[-T] { def render(value: T): String }
given StringRender: Render[String] with
def render(value: String): String = value
given UndefOrRender: Render[scala.|[String, Unit]] with
def render(value: scala.|[String, Unit]): String =
if value == () then "empty" else value.asInstanceOf[String]
implicitly[Render[String]] |
Compiler version
3.1.1-RC1
, ScalaJSMinimized code
https://scastie.scala-lang.org/mVuFoqtXTbi89svihWkgeg
Output
Expectation
Should compile like in Scala
2.13
Notes
It works when making the trait
Render
invariant.It also works when using
implicit val
instead ofimplicit object
:https://scastie.scala-lang.org/mVuFoqtXTbi89svihWkgeg
The text was updated successfully, but these errors were encountered: