public
Last active

Access to companion object of Foo via implicit resolution

  • Download Gist
gistfile1.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
trait Companion[T] {
type C
def apply() : C
}
 
object Companion {
implicit def companion[T](implicit comp : Companion[T]) = comp()
}
 
object TestCompanion {
trait Foo
object Foo {
def bar = "wibble"
// Per-companion boilerplate for access via implicit resolution
implicit def companion = new Companion[Foo] {
type C = Foo.type
def apply() = Foo
}
}
import Companion._
 
val fc = companion[Foo] // Type is Foo.type
val s = fc.bar // bar is accessible
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.