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

标签 scala playframework playframework-2.0

我是 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/

    相关文章:

    java - Ebean:奇怪的结果

    scala - 让 Scala 解释器在解释调用之间忘记

    forms - 玩2形式限制

    java - H2 sql如何将列设置为auto_increment

    mysql - 如何有条件地包含 Hibernate 注释?

    database - 在生产模式下访问 Play 框架的嵌入式 H2 数据库

    java - 在 java 中使用 play framewok 和 akka 映射诊断上下文日志记录

    scala - Play Framework 2.6 Alpakka S3 文件上传

    scala - Snappy-java 解压缩有效数据失败

    Scala 隐式搜索