Skip to content

Instantly share code, notes, and snippets.

@Mortimerp9
Created May 25, 2013 13:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Mortimerp9/5649109 to your computer and use it in GitHub Desktop.
Save Mortimerp9/5649109 to your computer and use it in GitHub Desktop.
"cheap" implementation of an immutable IndexedSeq backed by an array. `b` looks like an Array according to the types, but can't be modified nor cast back to a mutable Array.
import scala.reflect.ClassTag
import scala.collection.mutable.WrappedArray
import scala.collection.mutable.ArrayLike
def ASeq[T](elt: T*)(implicit ct: ClassTag[T]): IndexedSeq[T] = {
val a = elt.toArray.clone
a.deep.asInstanceOf[IndexedSeq[T]]
}
val a = Array(1,2,3) //> a : Array[Int] = Array(1, 2, 3)
val b = ASeq(a:_*) //> b : IndexedSeq[Int] = Array(1, 2, 3)
a(2) = 4
println(b) //> Array(1, 2, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment