You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Scala compiler resolves unbound method applications by searching for implicit conversions in companion objects along a type's linearization. Leveraging this you can create statically dispatched polymorphic methods by placing definitions in an implicit class in the companion object of each template in a hierarchy. You might rightly discourage such a design, but doing so allows for aggressive inlining and closure elimination in abstract interfaces without overly constraining implementations. It also lets you override methods with incompatible type signatures.
Scaladoc's new support for documenting implicits provides discoverability and clarity for such designs. Unfortunately, Scaladoc doesn't accurately reflect the compiler's interpretation of extension methods. The following code gives an example of this pattern and notes where Scaladoc misinterprets the definitions.
packagenonvirtualclassA[X] { overridedeftoString="A" }
objectA {
implicitclassAOps[X](val__:A[X]) extendsAnyVal {
deff1(x: Int) ="A"deff2(x: Int) =newA[X]
deff3[Y](x: Int) =newA[Y]
deff4[Y](f: X=>Y) ="A"
}
}
classB[X] extendsA[X] { overridedeftoString="B" }
objectB {
implicitclassBOps[X](val__:B[X]) extendsAnyVal {
// Scaladoc lists f1, f2, and f3 as shadowed in B rather than overridendeff1(x: Int) ="B"deff2(x: Int) =newB[X]
deff3[Y](x: Int) =newB[Y]
// Scaladoc redundantly lists AOps.f4 in B when only BOps.f4 should appeardeff4[Y](f: X=>Y) ="B"
}
}
objectTestextendsApp {
// no implicit resolution conflictsvala=newA[String]
valb=newB[String]
println("a.f1: "+ a.f1(0))
println("b.f1: "+ b.f1(0))
println("a.f2: "+ a.f2(0))
println("b.f2: "+ b.f2(0))
println("a.f3: "+ a.f3(0))
println("b.f3: "+ b.f3(0))
println("a.f4: "+ a.f4(identity))
println("b.f4: "+ b.f4(identity))
}
The test routine has no ambiguities and statically resolves method implementations as you would expect:
@VladUreche said (edited on Jul 20, 2012 1:49:56 PM UTC):
Hi Chris,
I see what you mean, declaring a member ambiguous or shadowed is done too eagerly. This was a conscious decision to clean up the collections list of members, but I agree shadowing and ambiguity are grossly overestimated. We should add a flag to use a more accurate shadowing and ambiguity check, but since that depends on the order of the conversions, it's a pretty big change. The current flag, -implicits-sound-shadowing, only changes the shadowing but not ambiguity resolution, so it won't fix the pattern above.
Chris Sachs (c9r) said:
Hi Vlad. I think you picked the right way to present implicits to users; separating shadowed and ambiguous members works well, and already does the right thing in the important cases. I love how the new diagrams show implicit conversions as well. I hope eventually you're able to work implicit overriding into Scaladoc's model. But for now I'm ecstatic to see implicits show up at all. Thanks.
The Scala compiler resolves unbound method applications by searching for implicit conversions in companion objects along a type's linearization. Leveraging this you can create statically dispatched polymorphic methods by placing definitions in an implicit class in the companion object of each template in a hierarchy. You might rightly discourage such a design, but doing so allows for aggressive inlining and closure elimination in abstract interfaces without overly constraining implementations. It also lets you override methods with incompatible type signatures.
Scaladoc's new support for documenting implicits provides discoverability and clarity for such designs. Unfortunately, Scaladoc doesn't accurately reflect the compiler's interpretation of extension methods. The following code gives an example of this pattern and notes where Scaladoc misinterprets the definitions.
The test routine has no ambiguities and statically resolves method implementations as you would expect:
I appreciate all the effort that's gone into making Scaladoc such a great tool. Keep up the good work!
The text was updated successfully, but these errors were encountered: