Skip to content

Simplify WrappedArray for boxed primitives #10851

@lrytz

Description

@lrytz

In 2.12, a mutable.ArraySeq wraps an Array[AnyRef], which means primitives are boxed in the underlying array.

WrappedArray by default uses unboxed arrays for primitives, but it can also wrap an array of boxed primitives.

scala> import scala.collection.mutable.WrappedArray
import scala.collection.mutable.WrappedArray

scala> val a = WrappedArray.make(Array(1))
a: scala.collection.mutable.WrappedArray[Int] = WrappedArray(1)

scala> a.array.getClass
res0: Class[_ <: Array[Int]] = class [I

scala> val a = WrappedArray.make(Array(1): Array[Any]).asInstanceOf[WrappedArray[Int]]
a: scala.collection.mutable.WrappedArray[Int] = WrappedArray(1)

scala> a.array.getClass
res1: Class[_ <: Array[Int]] = class [Ljava.lang.Object;

Can / should we make this easier to do?

Also, can we avoid this problem here:

scala> a.array
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [I

Once things are settled, update the docs at https://github.com/scala/collection-strawman/wiki/FAQ#new-deprecations

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions