【问题标题】:Scalalikejdbc Implicit ParameterScalikejdbc 隐式参数
【发布时间】:2020-02-24 16:03:19
【问题描述】:

尝试理解 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)
  }
} 

【问题讨论】:

    标签: scala scalikejdbc


    【解决方案1】:

    这意味着session 在整个function body 的隐式范围内,例如

    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 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      • 2012-12-30
      • 2013-07-20
      相关资源
      最近更新 更多