com.thoughtworks
An implicit value that points to the function caller.
libraryDependencies += "com.thoughtworks.feature" %% "caller" % "latest.release"
object Foo{ def log()(implicit caller: Caller[Any]) = { println(caller.value) } } object Bar{ Foo.log() // Bar }
class IKnowWhatImDoing object Foo{ def runDangerous()(implicit caller: Caller[IKnowWhatImDoing]) = { println(caller.value) } } object Bar { Foo.runDangerous() // compile error } object Bar2 extends IKnowWhatImDoing{ Foo.runDangerous() // ok, prints Bar2 }
object Foo{ def getResource(path: String)(implicit caller: Caller[_]) = { caller.value.getClass.getClassLoader.getResourceAsStream(path) } } object Bar{ Foo.getResource("/thing/file.txt") // loads resource from `Bar`s classloader, always }
Li Haoyi
杨博 (Yang Bo) <[email protected]>
An implicit value that points to the function caller.
Usage
Getting the caller for logging or something
Restricting who you can be called from
Getting calling class or classloader, e.g. for loading resources, without needing to worry about properly setting up and tearing down the Context ClassLoader
Authors:
Li Haoyi
杨博 (Yang Bo) <[email protected]>