The code is obvious, and I fail to see what’s wrong with it.
object TestX extends App {
val l= List[String]( "a","b","c" )
val n= l.size
def f( i:Int )= l[(i+1)%n ]
}
Compiler output (Scala 2.9.2):
fsc -deprecation -d bin src/xxx.scala
src/xxx.scala:11: error: identifier expected but integer literal found.
def f( i:Int )= l[(i+1)%n]
^
Brackets
[,]in Scala are used to declare or apply type parameters. Getting an element in a sequence or array is theapply(index: Int)method whereapplymay be omitted. Thus:or short
Note that
applyandsizeon aListtake time O(N), so if you need those operations often for large lists, consider usingVectorinstead.