I have seen this kind of code many times before, most recently at scala-user mailing list:
context(GUI) { implicit ec =>
// some code
}
context
is defined as:
def context[T](ec: ExecutionContext)(block: ExecutionContext => T): Unit = {
ec execute {
block(ec)
}
}
What purpose does the keeyword implicit
achieve when placed in front of a lambda expression parameter?
scala> trait Conn
defined trait Conn
scala> def ping(implicit c: Conn) = true
ping: (implicit c: Conn)Boolean
scala> def withConn[A](f: Conn => A): A = { val c = new Conn{}; f(c); /*cleanup*/ }
withConn: [A](f: Conn => A)A
scala> withConn[Boolean]( c => ping(c) )
res0: Boolean = true
scala> withConn[Boolean]{ c => implicit val c1 = c; ping }
res1: Boolean = true
scala> withConn[Boolean]( implicit c => ping )
res2: Boolean = true
The last line is essentially a shorthand for the second last.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With