KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Suppose we have implicit parameter lookup concerning only local scopes: trait CanFoo[A] { def foos(x: A): String } object Def { implicit object ImportIntFoo extends CanFoo[Int] { def foos(x: Int) = "ImportIntFoo:" + x.toString } } object Main { def test(): String = { implicit object LocalIntFoo extends CanFoo[Int] { def foos(x: Int) = "LocalIntFoo:" + x.toString } import Def._ foo(1) } def foo[A:CanFoo](x: A): String = implicitly[CanFoo[A]].foos(x) } In the above code, LocalIntFoo wins over ImportedIntFoo . Could someone explain how it's considered more specific using "the rules of static overloading resolution (§6.26.3)"? Edit : The name binding precedence is a compelling argument, but there are several issues unresolved. First, Scala Language Reference says: If there are several eligible arguments which match the implicit parameter’s type, a most specific one will be chosen using the rules of static overloading resolution (§6.26.3). Second, name binding precedence is about resolving a known identifier x to a particular member pkg.A.B.x in case there are several variable/method/object named x in the scope. ImportIntFoo and LocalIntFoo are not named the same. Third, I can show that name binding precedence alone is not in play as follows: trait CanFoo[A] { def foos(x: A): String } object Def { implicit object ImportIntFoo extends CanFoo[Int] { def foos(x: Int) = "ImportIntFoo:" + x.toString } } object Main { def test(): String = { implicit object LocalAnyFoo extends CanFoo[Any] { def foos(x: Any) = "LocalAnyFoo:" + x.toString } // implicit object LocalIntFoo extends CanFoo[Int] { // def foos(x: Int) = "LocalIntFoo:" + x.toString // } impo
Tags (comma-separated)
Save Edits
Cancel