gpt4 book ai didi

scala - 这个示例Play框架代码中unapply有什么用(表单映射)

转载 作者:行者123 更新时间:2023-12-04 03:58:55 25 4
gpt4 key购买 nike

我是 Play 框架的新手,正在浏览 2.1 samples ,并在 computer-database例如,我发现了以下我不完全理解的表单定义。
Computer.apply的作用是什么?和 Computer.unapply这里?

  val computerForm = Form(
mapping(
"id" -> ignored(NotAssigned:Pk[Long]),
"name" -> nonEmptyText,
"introduced" -> optional(date("yyyy-MM-dd")),
"discontinued" -> optional(date("yyyy-MM-dd")),
"company" -> optional(longNumber)
)(Computer.apply)(Computer.unapply)
)

(来自 controllers/Application.scala)

编辑 :这似乎是一个很好的资源: https://groups.google.com/forum/?fromgroups=#!topic/play-framework/dxNQ8E81YJs但仍然不确定我是否完全了解大局。

最佳答案

您应该了解 Scala 上下文中应用/取消应用的内容,因为它不是特定于 Play2/forms 的。

  • A Tour of Scala: Extractor Objects
  • Scala extractors

  • In the apply method we take the required parameters and return the new instance of class we are interested in where as we do the reverse in unapply- we take the instance and extract out the required information and return them in the form of a tuple.



    简而言之: apply用于从参数构造计算机对象。 unapply用于相反的情况,从 Computer 对象中提取参数。

    关于scala - 这个示例Play框架代码中unapply有什么用(表单映射),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14593589/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com