Skip to content

Instantly share code, notes, and snippets.

@sujithjay
Forked from Mortimerp9/gist:5649109
Last active March 13, 2018 11:52
Show Gist options
  • Save sujithjay/e974c36249c657fda5dcbeb955297ab2 to your computer and use it in GitHub Desktop.
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.
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