使用具有大于 22 个参数的 Play Framework 和 case class

Using Play Framework and case class with greater than 22 parameters

我已经看到一些其他问题涉及臭名昭著的“22 fields/parameters”问题,这是 Scala V < 2.11 的固有错误(功能?)。看到here and . However, as per this blog post,看来case class中的22个参数限制已经修复了;至少在语言方面。

我有一个 case class,我想加载任意(读取:> 22)个值,稍后将使用 Play 库将其读入 JSON 对象。

看起来像这样:

object L {
  import play.api.libs.json.Reads. _
  import play.api.libs.functional.syntax._

  implicit val responseRead: Reads[L] = (
    MyField1.jsPath.Read[MyField1.t] and 
    MyField2.jsPath.Read[MyField2.t] and
    ...
    MyField35.jsPath.Read[MyField35.t]
  ) (L.apply _)
}

case class L(myField1: MyField1.t, myField2: MyField2.t, ... myField35: MyField35.t)

问题是在编译时,Scala 抱怨 case class 中的参数超过 22 个。 (特别是:在对象定义的最后一行,当编译器尝试构建时,我得到:"implementation restricts functions to 22 parameters"。)我目前使用的是 Scala v2.11.6,所以我 认为 这不是语言问题。这让我觉得 Play 库还没有更新他们对 Read.

的实现

如果是这样,那么我想最好的办法是将相关字段分组到元组中,然后通过 JSON API?

传递元组

正如您引用的博客 post 中所述,22 个参数的限制对于 Scala 2.11 及更高版本中的函数仍然有效,因此您遇到的 语言问题。这种情况下的函数调用是:

L.apply _

重构模型是解决此限制的一种方法。

所以这个问题的答案实际上是两部分:

1。解决方法

我将其称为 "workaround" 因为虽然它 "work" 它通常解决的是症状而不是问题。

我的解决方案是使用 shapeless 提供任意长度的通用异构列表。该解决方案已在其他地方广泛讨论并可用。参见,例如,(1) [SO Post] How to get around the Scala case class limit of 22 fields?; (2) Blog post; (3) Yet another blog post.

2。解决方案

正如@jeffrey-chung 提到的那样,重构模型来处理这个限制。正如业内许多人所指出的那样,拥有超过 30 个参数的函数可能表明您的函数做得太多,或者应该重构该函数以摄取较少数量的参数。参见,例如,(1) Rule of 30 – When is a method, class or subsystem too big?; (2) Databrick's style guide.

在这里查看答案

看来这一切都处理得很好。

+22 field case class 格式化程序和更多播放器-json https://github.com/xdotai/play-json-extensions

支持 Scala 2.11.x、2.12.x 和 2.13.x 并支持 2.3、2.4、2.5 和 2.7

并在 play-json issue 中被引用为首选解决方案(但尚未合并)