Package

com.thoughtworks

feature

Permalink

package feature

Visibility
  1. Public
  2. All

Type Members

  1. final class Caller[+A] extends AnyVal

    Permalink

    An implicit value that points to the function caller.

    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
    }
    Authors:

    Li Haoyi

    杨博 (Yang Bo) <[email protected]>

Value Members

  1. object Caller

    Permalink

Ungrouped