如何绕过 22 个字段的 Scala 案例类限制? [英] How to get around the Scala case class limit of 22 fields?

查看:45
本文介绍了如何绕过 22 个字段的 Scala 案例类限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala 案例类在构造函数中限制为 22 个字段.我想超过这个限制,有没有办法通过继承或组合来处理案例类?

解决方案

最近(2016 年 10 月,OP 六年后),博客文章Scala 和 22" 来自 Richard Dallaway 探索了这个限制:

<块引用>

早在 2014 年,Scala 2.11 发布时,就移除了一个重要的限制:

带有 > 的案例类现在允许使用 22 个参数.

也就是说,案例类字段的数量仍然存在限制,请参阅https://stackoverflow.com/a/55498135/1586965

<块引用>

这可能会让您认为 Scala 中没有 22 限制,但事实并非如此.限制存在于函数和元组中.

Scala 2.11 中引入的修复 (PR 2305) 消除了上述限制常见场景:构建案例类、字段访问(包括复制)和模式匹配(baring edge case).

它通过为超过 22 个字段的案例类省略 unapplytupled 来实现这一点.
换句话说,限制 Function22Tuple22 仍然存在.

解决限制(Scala 2.11 之后)

有两种常见的技巧可以绕过这个限制.

  • 首先是使用嵌套元组.
    虽然元组确实不能包含超过 22 个元素,但每个元素本身都可以是一个元组

  • 另一个常见的技巧是使用异构列表 (HLList),其中没有 22 个限制.

如果你想使用 case 类,你最好使用 shapeless HList 实现.我们创建了 Slickless 库 来简化此操作.特别是 最近的 mappedWith 方法 在shapeless HLLists 和 case 类.它看起来像这样:

import slick.driver.H2Driver.api._进口无形._进口不光滑._class LargeTable(tag: Tag) extends Table[Large](tag, "large") {def a = column[Int]("a")def b = column[Int]("b")def c = column[Int]("c")/* 等等 */def u = column[Int]("u")def v = column[Int]("v")def w = column[Int]("w")def * = (a :: b :: c ::/* etc */:: u :: v :: w :: HNil).mappedWith(通用[大])}

<块引用>

有一个 完整示例,包含 26 列 在 Slickless 代码库中.

Scala case classes have a limit of 22 fields in the constructor. I want to exceed this limit, is there a way to do it with inheritance or composition that works with case classes?

解决方案

More recently (Oct 2016, six years after the OP), the blog post "Scala and 22" from Richard Dallaway explores that limit:

Back in 2014, when Scala 2.11 was released, an important limitation was removed:

Case classes with > 22 parameters are now allowed. 

That said, there still exists a limit on the number of case class fields, please see https://stackoverflow.com/a/55498135/1586965

This may lead you to think there are no 22 limits in Scala, but that’s not the case. The limit lives on in functions and tuples.

The fix (PR 2305) introduced in Scala 2.11 removed the limitation for the above common scenarios: constructing case classes, field access (including copying), and pattern matching (baring edge cases).

It did this by omitting unapply and tupled for case classes above 22 fields.
In other words, the limit to Function22 and Tuple22 still exists.

Working around the Limit (post Scala 2.11)

There are two common tricks for getting around this limit.

  • The first is to use nested tuples.
    Although it’s true a tuple can’t contain more than 22 elements, each element itself could be a tuple

  • The other common trick is to use heterogeneous lists (HLists), where there’s no 22 limit.

If you want to make use of case classes, you may be better off using the shapeless HList implementation. We’ve created the Slickless library to make that easier. In particular the recent mappedWith method converts between shapeless HLists and case classes. It looks like this:

import slick.driver.H2Driver.api._
import shapeless._
import slickless._

class LargeTable(tag: Tag) extends Table[Large](tag, "large") {
  def a = column[Int]("a")
  def b = column[Int]("b")
  def c = column[Int]("c")
  /* etc */
  def u = column[Int]("u")
  def v = column[Int]("v")
  def w = column[Int]("w")

  def * = (a :: b :: c :: /* etc */ :: u :: v :: w :: HNil)
    .mappedWith(Generic[Large])
}

There’s a full example with 26 columns in the Slickless code base.

这篇关于如何绕过 22 个字段的 Scala 案例类限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆