Shapeless Generic和case类toArray异常,具体取决于字段类型

时间:2014-09-16 17:23:14

标签: scala shapeless

我遇到了以下令人费解的行为 (Scala 2.10.4无形2.0)

import shapeless._
import poly._

object TypeMapper extends Poly1 {
  implicit def caseInt     = at[Int](identity)
  implicit def caseLong    = at[Long](identity)
  implicit def caseString  = at[String](identity)
}


case class L2(x: String, y: Long)
val l2Gen = Generic[L2]
val l2 = l2Gen.to(L2("A",1L)).map(TypeMapper).toArray

case class LL3( c: Long, a: String, b: String)
val ll3Gen = Generic[LL3]
val ll3 = ll3Gen.to(LL3(1L,"A","B")).map(TypeMapper).toArray

这两个工作都是希望的,但是当我有一个具有以下字段布局的案例类

case class SL3(a: String, b: String, c: Long)
val sl3Gen = Generic[SL3]
val sl3 = sl3Gen.to(SL3("A","B",1L)).map(TypeMapper).toArray

代码爆炸

java.lang.ArrayIndexOutOfBoundsException: 1
    at shapeless.ops.hlist$LowPriorityToArray$$anon$103.loop$2(hlists.scala:595)
    at shapeless.ops.hlist$LowPriorityToArray$$anon$103.apply(hlists.scala:598)
    at shapeless.ops.hlist$LowPriorityToArray$$anon$103.apply(hlists.scala:589)
    at shapeless.syntax.HListOps.toArray(hlists.scala:439)
    at .<init>(<console>:17)
    at .<clinit>(<console>)
    at .<init>(<console>:7)
    at .<clinit>(<console>)
    at $print(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
    at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
    at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
    at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:760)
    at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:805)
    at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:717)
    at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:581)
    at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:588)
    at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:591)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:882)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:837)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:837)
    at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
    at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:837)
    at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:904)
    at xsbt.ConsoleInterface.run(ConsoleInterface.scala:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:102)
    at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:77)
    at sbt.Console.sbt$Console$$console0$1(Console.scala:23)
    at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
    at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply(Console.scala:24)
    at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply(Console.scala:24)
    at sbt.Logger$$anon$4.apply(Logger.scala:90)
    at sbt.TrapExit$App.run(TrapExit.scala:244)
    at java.lang.Thread.run(Thread.java:744)

任何人都可以解释为什么会发生这种情况,并且(希望如何)避免这种情况

1 个答案:

答案 0 :(得分:0)

这是无形2.0.0中的一个错误,它在2.1.0-SNAPSHOT中修复。 Github问题here