Skip to content

Add unsafeToArray to IArray #10730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions library/src/scala/IArray.scala
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ object opaques:
extension [T](arr: IArray[T]) def takeWhile(p: T => Boolean): IArray[T] =
genericArrayOps(arr).takeWhile(p)

/** Returns the underlying _mutable_ array; use with caution. Like `unsafeFromArray`,
* the returned array must _not_ be mutated after this or the guaranteed immutablity
* of IArray will be violated.
*/
extension [T](arr: IArray[T]) def unsafeToArray: Array[T] =
arr.asInstanceOf[Array[T]]

/** Converts an array of pairs into an array of first elements and an array of second elements. */
extension [U: ClassTag, V: ClassTag](arr: IArray[(U, V)]) def unzip: (IArray[U], IArray[V]) =
genericArrayOps(arr).unzip
Expand Down
7 changes: 6 additions & 1 deletion tests/run/iarrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,9 @@ object Test extends App {
val cs: Array[Double] = bs.asInstanceOf[Array[Double]]
cs(1) = 3.0
assert(bs(1) == 3.0)
}

// Check unsafeFromArray andThen unsafeToArray is an identity
val ds: Array[Double] = Array(1.0, 2.0)
val es: Array[Double] = IArray.unsafeFromArray(ds).unsafeToArray
assert(ds eq es)
}