Skip to content

Instantly share code, notes, and snippets.

@japgolly
Last active August 29, 2015 14:07
Show Gist options
  • Save japgolly/896e79c1f6df26e5f9a9 to your computer and use it in GitHub Desktop.
Save japgolly/896e79c1f6df26e5f9a9 to your computer and use it in GitHub Desktop.
Scala implicit search path
package lib {
trait O[A]
object O {
import F.I
implicit val Il = null.asInstanceOf[I[Long]]
implicit val Ol = null.asInstanceOf[O[Long]]
}
trait T {
import F.I
implicit val Is = null.asInstanceOf[I[String]]
implicit val Os = null.asInstanceOf[O[String]]
}
object F extends T {
trait I[A]
implicit val Ii = null.asInstanceOf[I[Int]]
implicit val Oi = null.asInstanceOf[O[Int]]
def testI[A:I] = ()
def testO[A:O] = ()
}
}
package test {
import lib._
object Test {
// Compiles
F.testI[Int] // T & TC declared in same object.
F.testO[Long] // TC declared in companion object of T.
F.testI[String] // TC mixed into same object in which T is declared.
/* Doesn't compile
F.testO[Int] // TC in different object than T.
F.testI[Long] // TC in different object than T.
F.testO[String] // TC in different object than T.
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment