加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何解决Scala案例限制的22个字段?

发布时间:2020-12-16 09:41:49 所属栏目:安全 来源:网络整理
导读:Scala案例类在构造函数中有22个字段的限制。我想超过这个限制,有没有办法用继承或组合来处理案例类? 解决方法 最近(2016年10月,OP后六年), Richard Dallaway 的博客文章“ Scala and 22 ”探讨了这个限制: Back in 2014,when Scala 2.11 was released,a
Scala案例类在构造函数中有22个字段的限制。我想超过这个限制,有没有办法用继承或组合来处理案例类?

解决方法

最近(2016年10月,OP后六年), Richard Dallaway的博客文章“ Scala and 22”探讨了这个限制:

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

Case classes with > 22 parameters are now allowed.

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 (07002) introduced in Scala 2.11 removed the limitation for the above common scenarios: constructing case classes,field access (including copying),and pattern matching (07003).

It did this by omitting unapply and tupled for case classes above 22 fields.
In other words,the limit to 07004 and 07005 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 07006 to make that easier. In particular 07007 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 07008 in the Slickless code base.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读