-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Derived type class instances do not seem to be eligible for extension methods #5773
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 works as well: val optSG = implicitly[Semigroup[Option[Int]]]
val some5 = {
implicit val impSG = optSG
Some(2) combine Some(3)
} Implicit classes seem to not have this problem: implicit class RichSemigrouped[SG: Semigroup](x: SG){
def combine2(y: SG) = x combine y
}
val some5 = Some(3) combine2 Some(2) //still doesn't work
val optionInt: Option[Int] = Some(3)
val x1 = optionInt combine Some(2) // doesn't work
val x2 = optionInt combine2 Some(2) // works |
trait Semigroup[T] {
def (lhs: T) append (rhs: T): T
}
object Semigroup {
implicit object stringAppend extends Semigroup[String] {
override def (lhs: String) append (rhs: String): String = lhs + rhs
}
implicit def sumSemigroup[N](implicit N: Numeric[N]): Semigroup[N] = new {
override def (lhs: N) append (rhs: N): N = N.plus(lhs, rhs)
}
}
object Main {
def main(args: Array[String]): Unit = {
import Semigroup.stringAppend // necessary to make the extension method visible
println("Hi" append " mum")
import Semigroup.sumSemigroup // this is not sufficient
println(1 append 2) // this won't compile
implicit val intSumAppend: Semigroup[Int] = sumSemigroup[Int]
println(3 append 4) // this will
}
} |
This is a tough one. The problem is that, relative to implicit classes search order for the 1 append 2 will expand to Semigroup.sumSemigroup[N](implicit N: Numeric[N]) At that point we are forced to do an implicit search which will fail with an ambiguous result, since
A possible fix would be to move the implicit from the |
When retrying after an ambiguous implicit, we now make use of all the information in the prototype, including ignored parts. We also try to match formal parameters with actually given arguments. Fixes scala#11243 Fixes scala#5773, which previously was closed with a more detailed error message.
When retrying after an ambiguous implicit, we now make use of all the information in the prototype, including ignored parts. We also try to match formal parameters with actually given arguments. Fixes scala#11243 Fixes scala#5773, which previously was closed with a more detailed error message.
When retrying after an ambiguous implicit, we now make use of all the information in the prototype, including ignored parts. We also try to match formal parameters with actually given arguments. Fixes scala#11243 Fixes scala#5773, which previously was closed with a more detailed error message.
When retrying after an ambiguous implicit, we now make use of all the information in the prototype, including ignored parts. We also try to match formal parameters with actually given arguments. Fixes scala#11243 Fixes scala#5773, which previously was closed with a more detailed error message.
See:
This works:
But not this:
not this:
But this does:
The text was updated successfully, but these errors were encountered: