@@ -3,14 +3,12 @@ import reflect.ClassTag
3
3
4
4
import scala .collection .immutable
5
5
6
+ opaque type IArray [+ T ] = Array [_ <: T ]
7
+
6
8
/** An immutable array. An `IArray[T]` has the same representation as an `Array[T]`,
7
9
* but it cannot be updated. Unlike regular arrays, immutable arrays are covariant.
8
10
*/
9
- object opaques :
10
- opaque type IArray [+ T ] = Array [_ <: T ]
11
-
12
- private [scala] type Sub [A ] >: Array [A ] <: IArray [A ]
13
- private [scala] type Sup [A ] >: IArray [A ] <: Array [_ <: A ]
11
+ object IArray :
14
12
15
13
/** The selection operation on an immutable array.
16
14
*
@@ -316,17 +314,6 @@ object opaques:
316
314
extension (arr : IArray [Unit ]) def toSeq : immutable.ArraySeq [Unit ] =
317
315
immutable.ArraySeq .ofUnit(arr.asInstanceOf [Array [Unit ]])
318
316
319
- end opaques
320
-
321
- type IArray [+ T ] = opaques.IArray [T ]
322
-
323
- object IArray {
324
- import opaques .Sub
325
- import opaques .Sup
326
-
327
- // A convenience to avoid having to cast everything by hand
328
- private given [A ]: Conversion [Array [A ], IArray [A ]] = identity[Sub [A ]]
329
-
330
317
/** Convert an array into an immutable array without copying, the original array
331
318
* must _not_ be mutated after this or the guaranteed immutablity of IArray will
332
319
* be violated.
@@ -356,25 +343,25 @@ object IArray {
356
343
def emptyObjectIArray : IArray [Object ] = Array .emptyObjectArray
357
344
358
345
/** An immutable array with given elements. */
359
- inline def apply [T ](inline xs : T * )(using inline ct : ClassTag [T ]): IArray [T ] = Array (xs : _* ). asInstanceOf
346
+ def apply [T ](xs : T * )(using ct : ClassTag [T ]): IArray [T ] = Array (xs : _* )
360
347
/** An immutable array with given elements. */
361
- inline def apply (inline x : Boolean , inline xs : Boolean * ): IArray [Boolean ] = Array (x, xs : _* ). asInstanceOf
348
+ def apply (x : Boolean , xs : Boolean * ): IArray [Boolean ] = Array (x, xs : _* )
362
349
/** An immutable array with given elements. */
363
- inline def apply (inline x : Byte , inline xs : Byte * ): IArray [Byte ] = Array (x, xs : _* ). asInstanceOf
350
+ def apply (x : Byte , xs : Byte * ): IArray [Byte ] = Array (x, xs : _* )
364
351
/** An immutable array with given elements. */
365
- inline def apply (inline x : Short , inline xs : Short * ): IArray [Short ] = Array (x, xs : _* ). asInstanceOf
352
+ def apply (x : Short , xs : Short * ): IArray [Short ] = Array (x, xs : _* )
366
353
/** An immutable array with given elements. */
367
- inline def apply (inline x : Char , inline xs : Char * ): IArray [Char ] = Array (x, xs : _* ). asInstanceOf
354
+ def apply (x : Char , xs : Char * ): IArray [Char ] = Array (x, xs : _* )
368
355
/** An immutable array with given elements. */
369
- inline def apply (inline x : Int , inline xs : Int * ): IArray [Int ] = Array (x, xs : _* ). asInstanceOf
356
+ def apply (x : Int , xs : Int * ): IArray [Int ] = Array (x, xs : _* )
370
357
/** An immutable array with given elements. */
371
- inline def apply (inline x : Long , inline xs : Long * ): IArray [Long ] = Array (x, xs : _* ). asInstanceOf
358
+ def apply (x : Long , xs : Long * ): IArray [Long ] = Array (x, xs : _* )
372
359
/** An immutable array with given elements. */
373
- inline def apply (inline x : Float , inline xs : Float * ): IArray [Float ] = Array (x, xs : _* ). asInstanceOf
360
+ def apply (x : Float , xs : Float * ): IArray [Float ] = Array (x, xs : _* )
374
361
/** An immutable array with given elements. */
375
- inline def apply (inline x : Double , inline xs : Double * ): IArray [Double ] = Array (x, xs : _* ). asInstanceOf
362
+ def apply (x : Double , xs : Double * ): IArray [Double ] = Array (x, xs : _* )
376
363
/** An immutable array with given elements. */
377
- inline def apply (inline x : Unit , inline xs : Unit * ): IArray [Unit ] = Array (x, xs : _* ). asInstanceOf
364
+ def apply (x : Unit , xs : Unit * ): IArray [Unit ] = Array (x, xs : _* )
378
365
379
366
/** Concatenates all arrays into a single immutable array.
380
367
*
@@ -405,7 +392,7 @@ object IArray {
405
392
*/
406
393
def fill [T : ClassTag ](n1 : Int , n2 : Int )(elem : => T ): IArray [IArray [T ]] =
407
394
// We cannot avoid a cast here as Array.fill creates inner arrays out of our control:
408
- Array .fill(n1, n2)(elem). asInstanceOf
395
+ Array .fill(n1, n2)(elem)
409
396
410
397
/** Returns a three-dimensional immutable array that contains the results of some element computation a number
411
398
* of times. Each element is determined by a separate computation.
@@ -416,7 +403,7 @@ object IArray {
416
403
* @param elem the element computation
417
404
*/
418
405
def fill [T : ClassTag ](n1 : Int , n2 : Int , n3 : Int )(elem : => T ): IArray [IArray [IArray [T ]]] =
419
- Array .fill(n1, n2, n3)(elem). asInstanceOf
406
+ Array .fill(n1, n2, n3)(elem)
420
407
421
408
/** Returns a four-dimensional immutable array that contains the results of some element computation a number
422
409
* of times. Each element is determined by a separate computation.
@@ -428,7 +415,7 @@ object IArray {
428
415
* @param elem the element computation
429
416
*/
430
417
def fill [T : ClassTag ](n1 : Int , n2 : Int , n3 : Int , n4 : Int )(elem : => T ): IArray [IArray [IArray [IArray [T ]]]] =
431
- Array .fill(n1, n2, n3, n4)(elem). asInstanceOf
418
+ Array .fill(n1, n2, n3, n4)(elem)
432
419
433
420
/** Returns a five-dimensional immutable array that contains the results of some element computation a number
434
421
* of times. Each element is determined by a separate computation.
@@ -441,7 +428,7 @@ object IArray {
441
428
* @param elem the element computation
442
429
*/
443
430
def fill [T : ClassTag ](n1 : Int , n2 : Int , n3 : Int , n4 : Int , n5 : Int )(elem : => T ): IArray [IArray [IArray [IArray [IArray [T ]]]]] =
444
- Array .fill(n1, n2, n3, n4, n5)(elem). asInstanceOf
431
+ Array .fill(n1, n2, n3, n4, n5)(elem)
445
432
446
433
/** Returns an immutable array containing values of a given function over a range of integer
447
434
* values starting from 0.
@@ -460,7 +447,7 @@ object IArray {
460
447
* @param f The function computing element values
461
448
*/
462
449
def tabulate [T : ClassTag ](n1 : Int , n2 : Int )(f : (Int , Int ) => T ): IArray [IArray [T ]] =
463
- Array .tabulate(n1, n2)(f). asInstanceOf
450
+ Array .tabulate(n1, n2)(f)
464
451
465
452
/** Returns a three-dimensional immutable array containing values of a given function
466
453
* over ranges of integer values starting from `0`.
@@ -471,7 +458,7 @@ object IArray {
471
458
* @param f The function computing element values
472
459
*/
473
460
def tabulate [T : ClassTag ](n1 : Int , n2 : Int , n3 : Int )(f : (Int , Int , Int ) => T ): IArray [IArray [IArray [T ]]] =
474
- Array .tabulate(n1, n2, n3)(f). asInstanceOf
461
+ Array .tabulate(n1, n2, n3)(f)
475
462
476
463
/** Returns a four-dimensional immutable array containing values of a given function
477
464
* over ranges of integer values starting from `0`.
@@ -483,7 +470,7 @@ object IArray {
483
470
* @param f The function computing element values
484
471
*/
485
472
def tabulate [T : ClassTag ](n1 : Int , n2 : Int , n3 : Int , n4 : Int )(f : (Int , Int , Int , Int ) => T ): IArray [IArray [IArray [IArray [T ]]]] =
486
- Array .tabulate(n1, n2, n3, n4)(f). asInstanceOf
473
+ Array .tabulate(n1, n2, n3, n4)(f)
487
474
488
475
/** Returns a five-dimensional immutable array containing values of a given function
489
476
* over ranges of integer values starting from `0`.
@@ -496,7 +483,7 @@ object IArray {
496
483
* @param f The function computing element values
497
484
*/
498
485
def tabulate [T : ClassTag ](n1 : Int , n2 : Int , n3 : Int , n4 : Int , n5 : Int )(f : (Int , Int , Int , Int , Int ) => T ): IArray [IArray [IArray [IArray [IArray [T ]]]]] =
499
- Array .tabulate(n1, n2, n3, n4, n5)(f). asInstanceOf
486
+ Array .tabulate(n1, n2, n3, n4, n5)(f)
500
487
501
488
/** Returns an immutable array containing a sequence of increasing integers in a range.
502
489
*
@@ -531,8 +518,7 @@ object IArray {
531
518
* @param x the selector value
532
519
* @return sequence wrapped in a [[scala.Some ]], if `x` is a Seq, otherwise `None`
533
520
*/
534
- def unapplySeq [T ](x : IArray [T ]) =
535
- // The double type ascription is currently needed,
536
- // for some reason (see: https://scastie.scala-lang.org/sSsmOhKxSKym405MgNRKqQ)
537
- Array .unapplySeq((x : Sup [T ]): Array [_ <: T ])
538
- }
521
+ def unapplySeq [T ](x : IArray [T ]): Array .UnapplySeqWrapper [_ <: T ] =
522
+ Array .unapplySeq(x)
523
+
524
+ end IArray
0 commit comments