Scalalikejdbc Implicit Parameter

61 Views Asked by At

Trying to understand the following syntax (the implicit session) in Scala:

def getDates(date: String): Option[String] = DB.readOnly { implicit session =>
 val date = "20201020"
}

Using the readOnly method from scalalikejdbc. Definition of method is:

def readOnly[A](execution: DBSession => A)(implicit context: CPContext = NoCPContext, settings: SettingsProvider = SettingsProvider.default): A = {
  val cp = connectionPool(context)
  using(cp.borrow()) { conn =>
    DB(conn, cp.connectionAttributes, settings).autoClose(false).readOnly(execution)
  }
} 
1

There are 1 best solutions below

0
Mario Galic On

It means session is in implicit scope for the entirety of the function body, for example

trait Foo
val foo = new Foo {}
def g(implicit foo: Foo) = ???
val f: Foo => String = implicit foo => {
  // foo is in implicit scope in the method body
  g // foo argument passed in to g implicitly 
}