fork(1) download
  1. import scala.collection.immutable.VectorBuilder
  2.  
  3. class Node
  4.  
  5. trait NodeList {
  6. def getLength: Int
  7. def item(index: Int): Node
  8. }
  9.  
  10. object NodeListSanity {
  11. implicit def nodeListToIndexedSeq(nodeList: NodeList): IndexedSeq[Node] = {
  12. val builder = new VectorBuilder[Node]
  13. for (index <- 0 to nodeList.getLength - 1) {
  14. builder += nodeList.item(index)
  15. }
  16. builder.result
  17. }
  18. }
  19.  
  20. object Main extends App {
  21. import NodeListSanity._
  22.  
  23. val list = new NodeList { def getLength = 2; def item(index: Int) = new Node }
  24.  
  25. println(list(0))
  26. println(list(1))
  27. println(list(2))
  28. }
  29.  
Runtime error #stdin #stdout #stderr 0.36s 383104KB
stdin
Standard input is empty
stdout
Node@a68180
Node@1803968
stderr
Exception in thread "main" java.lang.IndexOutOfBoundsException: 2
	at scala.collection.immutable.Vector.checkRangeConvert(Vector.scala:137)
	at scala.collection.immutable.Vector.apply(Vector.scala:127)
	at Main$delayedInit$body.apply(Main.scala:27)
	at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
	at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
	at scala.App$$anonfun$main$1.apply(App.scala:71)
	at scala.App$$anonfun$main$1.apply(App.scala:71)
	at scala.collection.immutable.List.foreach(List.scala:318)
	at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
	at scala.App$class.main(App.scala:71)
	at Main$.main(Main.scala:20)
	at Main.main(Main.scala)