-
-
Save sujithjay/e974c36249c657fda5dcbeb955297ab2 to your computer and use it in GitHub Desktop.
"Cheap" implementation of an immutable.IndexedSeq backed by an Array. The output of ASeq looks like an Array according to the types, but is not mutable 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