Created
May 25, 2013 13:50
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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