0

试图理解 Scala 中的以下语法(隐式会话):

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

使用来自 scalalikejdbc 的 readOnly 方法。方法的定义是:

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)
  }
} 
4

1 回答 1

2

这意味着session在整个函数体的隐式范围内,例如

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 
}
于 2020-02-24T16:16:51.717 回答