Created
January 30, 2012 17:47
-
-
Save milessabin/1705644 to your computer and use it in GitHub Desktop.
Access to companion object of Foo via implicit resolution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment