scala case class 这时候该咋用( 二 )
==================原回答===================Here we go:@ import shapeless._@ import syntax.std.traversable._@ import syntax.std.tuple._@ case class data(a: Int, b: Int, c: Int, d: Int, e: Int)defined class data@ type DATA = https://www.zhihu.com/api/v4/questions/41977032/Int :: Int :: Int :: Int :: Int :: HNildefined type DATA@ val arr ="1\\t2\\t3\\t4\\t5".split(\u0026#39;\\t\u0026#39;).map(_.toInt)arr: Array = Array(1, 2, 3, 4, 5) @ val myData = data.tupled(arr.toHList.get.tupled)myData: data = data(1, 2, 3, 4, 5)
■网友
https://issues.scala-lang.org/browse/SI-7296limit of 22 是因为apply和unapply的参数个数限制(FunctionN),不知道你怎么使用case class,不需要pattern matching的话,直接用constructor,不要用case class了;否则的话按照 @neo lin的做法(嵌套)去restructure,或换版本。------------------------------------------一般的ORM框架对injection data的做法就是用反射了 case class data(a: Int, b: Int, c: Int, d: Int, e: Int) val arr = "1\\t2\\t3\\t4\\t5".split(\u0026#39;\\t\u0026#39;).map(_.toInt) //调用constructor val ru = scala.reflect.runtime.universe val dataConstr = ru.runtimeMirror( getClass.getClassLoader ).reflectClass( ru.typeOf.typeSymbol.asClass ).reflectConstructor( ru.typeOf.declaration(ru.nme.CONSTRUCTOR).asMethod ) println(dataConstr(arr: _*)) //or 调用apply方法 val m = ru.runtimeMirror(getClass.getClassLoader) val methodApply = m.reflect( m.reflectModule(ru.typeOf.termSymbol.asModule).instance ).reflectMethod( ru.typeOf.declaration(ru.newTermName("apply")).asMethod ) println(methodApply(arr: _*))
■网友
忘了现在最新版有没有22个值的上限。用嵌套,或者合理划分你的类型。题主改问题了,请参考HList的那套答案。泻药了。
推荐阅读
- 环球车讯网|奔驰X-Class特别版发布,来自波兰改装厂的大手笔
- 趣头条|吸睛之作 梅赛德斯·奔驰X-Class推改装特别版 全车采用大量碳纤维
- 后视镜|全车碳纤维 梅赛德斯-奔驰X-Class特别版发布
- 网通社|波兰改装厂打造 全车内外碳纤维 梅赛德斯-奔驰 X-Class特别版
- Python中怎样在list中保存class类型的数据
- iPad Air Smart Case 值得购买吗
- 技术选型:golang, nodejs, scala, java, .net core. 那个好
- 用classpass模式做美发O2O可行吗?
- 奔驰|科技智能 梅赛德斯奔驰S-Class自动泊车
- class里面有空格的元素咋选择
