Scala function call

I found the following function calls in several frames that seem to me as if the framework extends some base classes. Some examples:

within(500 millis)

or

"Testcase description" in
  { .... }

The first example returns an object of duration with a duration of 500 milliseconds from akka, and the second returns the definition of a test table from scalatest.

I would like to know how this behavior is achieved and what it is called.

+3
source share
2 answers

This is done using the Pimp my library technique .

To add non-existing methods to a class, you define an implicit method that converts the objects of this class into objects of the class that has the method:

class Units(i: Int) {
  def millis = i
}

implicit def toUnits(i: Int) = new Units(i)


class Specs(s: String) {
  def in(thunk: => Unit) = thunk
}

implicit def toSpecs(s: String) = new Specs(s)

See also Where is Scala Looking for Implicits?

+10

,

within(500.millis)

"Testcase description".in({ ... })

, .

+1

All Articles