Skip to content

Instantly share code, notes, and snippets.

@jorgeortiz85
Created June 17, 2012 03:22
Show Gist options
  • Save jorgeortiz85/2943283 to your computer and use it in GitHub Desktop.
Save jorgeortiz85/2943283 to your computer and use it in GitHub Desktop.
dependent method types
trait CompanionProvider[T] {
type CompanionT
def provide: CompanionT
}; object CompanionProvider {
def apply[T](implicit c: CompanionProvider[T]): c.CompanionT = c.provide
}
object Foo {
def foo = "fooooo"
implicit val companionProvider = new CompanionProvider[Foo] {
override type CompanionT = Foo.type
override def provide: Foo.type = Foo
}
}; class Foo
class Indirection[T](implicit val c: CompanionProvider[T]) {
def indirect: c.CompanionT = c.provide
}
class Indirection2[T] {
def indirect(implicit c: CompanionProvider[T]): c.CompanionT = c.provide
}
// works
CompanionProvider[Foo].foo
// error: value foo is not a member of _1.c.CompanionT
new Indirection[Foo].indirect.foo
// works
new Indirection2[Foo].indirect.foo
@adriaanm
Copy link

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