Skip to content

Commit 9f82972

Browse files
committed
Add unsafeToArray to IArray
1 parent 663d334 commit 9f82972

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

library/src/scala/IArray.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ object opaques:
247247
extension [T](arr: IArray[T]) def takeWhile(p: T => Boolean): IArray[T] =
248248
genericArrayOps(arr).takeWhile(p)
249249

250+
/** Returns the underlying _mutable_ array; use with caution. Like `unsafeFromArray`,
251+
* the returned array must _not_ be mutated after this or the guaranteed immutablity
252+
* of IArray will be violated.
253+
*/
254+
extension [T](arr: IArray[T]) def unsafeToArray: Array[T] =
255+
arr.asInstanceOf[Array[T]]
256+
250257
/** Converts an array of pairs into an array of first elements and an array of second elements. */
251258
extension [U: ClassTag, V: ClassTag](arr: IArray[(U, V)]) def unzip: (IArray[U], IArray[V]) =
252259
genericArrayOps(arr).unzip

tests/run/iarrays.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,9 @@ object Test extends App {
7070
val cs: Array[Double] = bs.asInstanceOf[Array[Double]]
7171
cs(1) = 3.0
7272
assert(bs(1) == 3.0)
73-
}
73+
74+
// Check unsafeFromArray andThen unsafeToArray is an identity
75+
val ds: Array[Double] = Array(1.0, 2.0)
76+
val es: Array[Double] = IArray.unsafeFromArray(ds).unsafeToArray
77+
assert(ds eq es)
78+
}

0 commit comments

Comments
 (0)