final class Caller[+A] extends AnyVal
An implicit value that points to the function caller.
Usage
libraryDependencies += "com.thoughtworks.feature" %% "caller" % "latest.release"
Getting the caller for logging or something
object Foo{ def log()(implicit caller: Caller[Any]) = { println(caller.value) } } object Bar{ Foo.log() // Bar }
Restricting who you can be called from
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 }
Getting calling class or classloader, e.g. for loading resources, without needing to worry about properly setting up and tearing down the Context ClassLoader
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 }
- Source
- Caller.scala
Linear Supertypes
Type Hierarchy
Ordering
- Alphabetic
- By Inheritance
Inherited
- Caller
- AnyVal
- Any
Implicitly
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
Visibility
- Public
- All
Instance Constructors
-
new
Caller(value: A)
- value
The caller instance
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
##(): Int
- Definition Classes
- Any
- def +(other: String): String
- def ->[B](y: B): (Caller[A], B)
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
- def ensuring(cond: (Caller[A]) ⇒ Boolean, msg: ⇒ Any): Caller[A]
- def ensuring(cond: (Caller[A]) ⇒ Boolean): Caller[A]
- def ensuring(cond: Boolean, msg: ⇒ Any): Caller[A]
- def ensuring(cond: Boolean): Caller[A]
- def formatted(fmtstr: String): String
-
def
getClass(): Class[_ <: AnyVal]
- Definition Classes
- AnyVal → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
toString(): String
- Definition Classes
- Any
- val value: A
- def →[B](y: B): (Caller[A], B)