Scala:为什么此模式匹配代码会抛出 IndexOutOfBoundsException?
我正在查看一个教程,发现于 http://www.codecommit.com/blog/scala /scala-for-java-refugees-part-4
本教程来自 jan。 2008 年,我明白了,但我正在使用 Scala 2.8.0,如果这有什么区别的话。
class Color(val red:Int, val green:Int, val blue:Int)
case class Red(r:Int) extends Color(r, 0, 0)
case class Green(g:Int) extends Color(0, g, 0)
case class Blue(b:Int) extends Color(0, 0, b)
def printColor(c:Color) = c match {
case Red(v) => println("Red: " + v)
case Green(v) => println("Green: " + v)
case Blue(v) => println("Blue: " + v)
case col:Color => {
print("R: " + col.red + ", ")
print("G: " + col.green + ", ")
println("B: " + col.blue)
}
case null => println("Invalid color")
}
当将其输入解释器时,它会
在线程“main”java.lang.IndexOutOfBoundsException中产生异常 在 scala.collection.LinearSeqOptimized$class.apply(LinearSeqOptimized.scala:53) 在 scala.collection.immutable.List.apply(List.scala:45)
加上另外 185 行跟踪,然后解释器退出。
这个错误消息是什么意思,谁能告诉我上面的代码有什么问题?
I was checking out a tutorial, found at
http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-4
This tutorial is from jan. 2008, I see, but I'm using Scala 2.8.0 if that makes a difference.
class Color(val red:Int, val green:Int, val blue:Int)
case class Red(r:Int) extends Color(r, 0, 0)
case class Green(g:Int) extends Color(0, g, 0)
case class Blue(b:Int) extends Color(0, 0, b)
def printColor(c:Color) = c match {
case Red(v) => println("Red: " + v)
case Green(v) => println("Green: " + v)
case Blue(v) => println("Blue: " + v)
case col:Color => {
print("R: " + col.red + ", ")
print("G: " + col.green + ", ")
println("B: " + col.blue)
}
case null => println("Invalid color")
}
When entering this into the interpreter, it produces
Exception in thread "main" java.lang.IndexOutOfBoundsException
at scala.collection.LinearSeqOptimized$class.apply(LinearSeqOptimized.scala:53)
at scala.collection.immutable.List.apply(List.scala:45)
plus another 185 lines of tracing, and the interpreter exits.
What does this error message mean, and can anyone tell me what is wrong with the code above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的消息总是一个错误。具体来说https://lampsvn.epfl.ch/trac/scala/ticket/4025 .
A message like that is always a bug. Specifically https://lampsvn.epfl.ch/trac/scala/ticket/4025 .