Skip to content

Method with generic return type hides implicit overload #11018

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
kellen opened this issue Jul 12, 2018 · 1 comment
Closed

Method with generic return type hides implicit overload #11018

kellen opened this issue Jul 12, 2018 · 1 comment

Comments

@kellen
Copy link

kellen commented Jul 12, 2018

Overloading a method on a class A via an implicit class works:

class A {
  def and(i: Int): A = { println(i); this }
}
implicit class I(a: A) {
  def and(t: (String, _)): A = { println(t) ; a }
}
(new A).and("a" -> "b") // works

But if one of the methods takes a type param for the return type:

class A {
  def and[T <: A](i: Int): T = { println(i); this.asInstanceOf[T] }
}
implicit class I(a: A) {
  def and(t: (String, _)): A = { println(t) ; a }
}
(new A).and("a" -> "b") // fails

the following error occurs:

error: type mismatch;
 found   : (String, String)
 required: Int
       (new A).and("a" -> "b")

Even giving them the same return type fails:

class A {
  def and[T <: A](i: Int): T = { println(i); this.asInstanceOf[T] }
}
implicit class I(a: A) {
  def and[T <: A](t: (String, _)): T = { println(t) ; a.asInstanceOf[T] }
}
(new A).and("a" -> "b") // fails

with:

error: type mismatch;
 found   : (String, String)
 required: Int
       (new A).and("a" -> "b") // fails
@som-snytt
Copy link

Duplicates #9523

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

No branches or pull requests

3 participants