Skip to content

Implicit resolution differs from Scala 2 in presence of GADTs #5469

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

Closed
abgruszecki opened this issue Nov 19, 2018 · 5 comments · Fixed by #16807
Closed

Implicit resolution differs from Scala 2 in presence of GADTs #5469

abgruszecki opened this issue Nov 19, 2018 · 5 comments · Fixed by #16807

Comments

@abgruszecki
Copy link
Contributor

dotty compiles following code:

object O {
  sealed trait Box[T] { def value: T }
  final case class IntBox(value: Int) extends Box[Int]

  implicit def s1[T](implicit box: Box[T]): String = "generic: " + box.value
  implicit def s2(implicit box: Box[Int]): String = "specific: " + box.value

  def test[T](implicit box: Box[T]): String = box match {
    case IntBox(_) => implicitly[String]
  }
}

The implicit resolved in test is s1 (the generic one). scalac refuses to compile the code, saying that the implicit is ambiguous.

@milessabin
Copy link
Contributor

What are the chances of getting dotty to select s2 here?

@abgruszecki
Copy link
Contributor Author

@milessabin I checked and the following code also emits ambiguous warning (in scalac):

@ object O {
    sealed trait Box[T] { def value: T }
    final case class IntBox(value: Int) extends Box[Int]
  
    implicit def s1[T](implicit box: Box[T]): String = "generic: " + box.value
    implicit def s2(implicit box: Box[Int]): String = "specific: " + box.value
  
    implicit val box: Box[Int] = new IntBox(0)
    val str = implicitly[String]
  } 
cmd3.sc:9: ambiguous implicit values:
 both method s2 in object O of type (implicit box: ammonite.$sess.cmd3.O.Box[Int])String
 and method s1 in object O of type [T](implicit box: ammonite.$sess.cmd3.O.Box[T])String
 match expected type String
  val str = implicitly[String]
                      ^
Compilation Failed

which would mean that selecting s2 is the same as changing implicit resolution algorithm - likely out of scope for this issue.

@milessabin
Copy link
Contributor

milessabin commented Nov 22, 2018

Right, but surely that's because scalac isn't able to identify Box[Int] with IntBox?

@abgruszecki
Copy link
Contributor Author

I'm not familiar enough with implicit resolution to answer that. Frankly, I'm surprised that scalac does not prefer s2 in the second example.

@milessabin
Copy link
Contributor

milessabin commented Nov 22, 2018

Ahh ... I misread your example.

The reason scalac sees these as ambiguous is its odd (non-)handling of implicit arguments when computing the specificity of implicit methods ... see scala/bug#10526. I think this is a bug and I'd like to see it fixed: scala/scala#6139.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants