# SLC: Improve pattern matching on Array / IArray

**URL:** <https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574>\
**Category:** Scala Standard Library\
**Created:** [November 6, 2026, 4:37pm UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574 "2026-11-06T16:37:04Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![scalafan](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/scalafan/32/3367_2.png) [@scalafan](https://contributors.scala-lang.org/u/scalafan)\
**Post date:** [November 6, 2026, 4:37pm UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574/1 "2026-11-06T16:37:04Z")

</div>

I have recently migrated the codebase from `Vector` to `IArray`, because it is the perfect collection for so many use-cases. I have encountered many quirks and have reported the bugs to you. But there is one major one remaining.

Here I propose two improvements:

## 1: Spread matching should return IArray, not ArraySeq

```scala
array match {
   case IArray(a, middle*, b) => // middle is ArraySeq
}

```

The problem is not just the `ArraySeq` wrapper object, but how difficult is it to convert `ArraySeq` back to `IArray`, especially because the arrays of primitives have their specifics. I think the code is, but it might be wrong:

```scala
IArray.unsafeFromArray(
    middle.unsafeArray.asInstanceOf[Array[Int]]
  )

```

Imagine writing this every time, it is not pretty.

## 2: Support :+ destructuring

Since the `multiSpreads` SIP-70 improvement, this is less esential, but it would be a nice win for ortnogonality, especially when you are migrating the codebase from `Vector`.

I have heard an objection in the other thread that this might not be very performant, but to do it once in a while, it is okay. Assuming it uses `System.arrayCopy` under the hood, it beats all other collection types that support `:+`:

1. `ArraySeq`: Same performance
2. `Vector`: Might be very much slower for small collections of under 100 elements.
3. `List`: Absolutely terrible performance, yet it is allowed.

---

<div class="post-metadata">

**Author:** ![bishabosha](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/bishabosha/32/3101_2.png) [@bishabosha](https://contributors.scala-lang.org/u/bishabosha)\
**Post date:** [November 9, 2026, 8:33am UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574/2 "2026-11-09T08:33:37Z")

</div>

This would require a language change. `case IArray(a, middle*, b)` desugars to `val middle = IArray.unapplySeq(array).drop(1).dropRight(1)`.

According to the docs the result type of `unapplySeq` needs to have methods which return `Seq`.

On the conversion point, perhaps we could special case `IArray.from` for immutable ArraySeq with the same element class as the ClassTag.

---

<div class="post-metadata">

**Author:** ![scalafan](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/scalafan/32/3367_2.png) [@scalafan](https://contributors.scala-lang.org/u/scalafan)\
**Post date:** [November 9, 2026, 11:23am UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574/3 "2026-11-09T11:23:05Z")

</div>

Ah, this is quite disappointing that is has to be a `Seq`.

A zero-copy `IArray.from` specialization for `ArraySeq` sounds nice. Both are immutable, so that should be safe. And JDK JIT might be smart enough to eliminate the allocation.

---

<div class="post-metadata">

**Author:** ![bishabosha](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/bishabosha/32/3101_2.png) [@bishabosha](https://contributors.scala-lang.org/u/bishabosha)\
**Post date:** [November 9, 2026, 1:39pm UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574/4 "2026-11-09T13:39:54Z")

</div>

in 3.10 there is also `.to(IArray)`

---

<div class="post-metadata">

**Author:** ![scalafan](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/scalafan/32/3367_2.png) [@scalafan](https://contributors.scala-lang.org/u/scalafan)\
**Post date:** [November 12, 2026, 5:43am UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574/5 "2026-11-12T05:43:09Z")

</div>

So we established `case IArray(a, middle*, b)` always has to give `Seq`.

Is the same true for `+:` and `:+`, or could these give you `IArray`?

---

<div class="post-metadata">

**Author:** ![irfanstract](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/irfanstract/32/3480_2.png) [@irfanstract](https://contributors.scala-lang.org/u/irfanstract)\
**Post date:** [November 12, 2026, 9:33am UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574/6 "2026-11-12T09:33:07Z")

</div>

> [@scalafan](#):
>
> I have encountered many quirks and have reported the bugs to you. But there is one major one remaining.

`IArray` has some more catches like poor `==` (equality) and poor `.toString`.  
some [tried to migrate the compiler from `List` to `IArray` in #26391](https://github.com/scala/scala3/pull/26391), but later on switched to _value classes_ (`class Lst[+A](_: IArray[?<: A]) extends AnyVal`) instead. best of both worlds.

> `Lst` is now a value class instead of an opaque type alias. This way can get `==` , `hashCode` and `toString` with reasonable semantics.

---

<div class="post-metadata">

**Author:** ![scalafan](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/scalafan/32/3367_2.png) [@scalafan](https://contributors.scala-lang.org/u/scalafan)\
**Post date:** [November 15, 2026, 11:10am UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574/7 "2026-11-15T11:10:01Z")

</div>

Very interesting and unique approach, how did you even think of that?

I thought we are discouraging the use of AnyVal in Scala 3. There has been some previous discussion here how to introduce equals and hashCode and toString to opaque types. Should be possible. But yes, it is a problem, you can have IArray as a field in case class and suddenly equality does not work anymore.

Did you observe any disadvantages of your approach whatsoever? For example that the compiler failed to eliminate the wrapper somewhere? Or any strange issues regarding ClassTag. Does your wrapper work for IArray[T] where T is an opaque type of a primitive?

---

<div class="post-metadata">

**Author:** ![Ichoran](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ichoran/32/40_2.png) [@Ichoran](https://contributors.scala-lang.org/u/Ichoran)\
**Post date:** [November 15, 2026, 6:36pm UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574/8 "2026-11-15T18:36:20Z")

</div>

> [@scalafan](#):
>
> Did you observe any disadvantages of your approach whatsoever?

It’s all the usual disadvantages of AnyVal, of course. For instance, `Lst[Lst[Int]]` boxes the inner `Lst`. Any time you touch generic code, `Lst[Int]` goes into a box. `IArray[Int]` doesn’t, but you lose the ability to tell whether it is `Array[Int]` or `IArray[Int]` at runtime.

---

<div class="post-metadata">

**Author:** ![bishabosha](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/bishabosha/32/3101_2.png) [@bishabosha](https://contributors.scala-lang.org/u/bishabosha)\
**Post date:** [November 16, 2026, 8:47am UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574/9 "2026-11-16T08:47:09Z")

</div>

> [@scalafan](#):
>
> how to introduce equals and hashCode and toString to opaque types. Should be possible.

this is impossible to support parametrically, any time your opaque type value is stored into a variable of a wider type than it is (e.g. in default HashMap) then it would need to be boxed in a class that could point to those methods, so making the design reason (no boxing) for using opaque type useless.

unless you can opt out of ever widening the type?

it is unlikely that would pass, so type class based is really the way to go if you want a string for a specific purpose

---

<div class="post-metadata">

**Author:** ![MateuszKubuszok](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/mateuszkubuszok/32/3222_2.png) [@MateuszKubuszok](https://contributors.scala-lang.org/u/MateuszKubuszok)\
**Post date:** [November 16, 2026, 10:53am UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574/10 "2026-11-16T10:53:34Z")

</div>

You either:

- wrap with `AnyVal`, where unboxing is a conservative best effort, boxing whenever there is any risk you would actually have to pattern match against the wrapper type, or otherwise check its runtime type (which means _a lot_)
- using `opaque type` where there is no wrapper in the runtime, and you also cannot distinguish between the underlying type an `opaque type` - any `opaque type` of that underlying type! Since JVM does not distinguish them it has no way of overriding the behavior (because the only way to override a behavior is to wrap or create a subtype, and `opaque type` does neither)

It’s either enforcing types only in compile time, with no reliance of dynamic dispatch (passing type class around in an exception, but it’s still the compiler that takes care of providing the right instance to its use site) or a runtime type checking, or an overhead of boxing that carries the type information. A “smart approach” to this was tried (`AnyVal` magic) and it couldn’t be done good enough despite years of effort.

---

<div class="post-metadata">

**Author:** ![scalafan](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/scalafan/32/3367_2.png) [@scalafan](https://contributors.scala-lang.org/u/scalafan)\
**Post date:** [November 17, 2026, 3:07pm UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574/11 "2026-11-17T15:07:02Z")

</div>

It looks like waiting for Valhalla and then promptly integrating it into Scala seems like the best option.

---

<div class="post-metadata">

**Author:** ![MateuszKubuszok](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/mateuszkubuszok/32/3222_2.png) [@MateuszKubuszok](https://contributors.scala-lang.org/u/MateuszKubuszok)\
**Post date:** [November 17, 2026, 5:12pm UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574/12 "2026-11-17T17:12:03Z")

</div>

“Overriding defs” in `opaque type`s would disallow virtually any type parametric type:

- `Option`/`Some` checks equality by checking if values inside it returns `true` for `equals` - impossible to override without wrapping
- same is true for: `Either`/`Left`/`Right`
- and for comparing any collection (`Seq`/`List`/`Vector`)

If you used hash-based collections, they would look at the `hashcode` inherited from Object, also impossible to override without wrapping.

Printing any of these? `toString` from `Object`, you either wrap it or use a subtype that `override`s it.

From `AnyVal` pitfalls there was also the need to wrap in runtime if one passed a value to `Function1` (or any function) because you could always implement it as

```scala
val: Foo => Bar = {
  case Foo(value) => ...
}

```

“Widening the type” is not the constraint. AFAIR _any type parametric class_ would require you to wrap, because in the runtime there would be some bytecode after type erasure, that has to distinct value type from a raw primitive, and needs a runtime information for it. Also, any code already compiled, which uses `equals`/`toString`/`hashcode`/etc, would not suddenly learn that for some particular type it should not use these methods but call something else. It would call these methods defined on `java.lang.Object`, and the only way to override these would be to override def in a subtype of this class - which in the idea above require wrapping it in runtime.

I don’t remember all the details from `AnyVal` gotchas, but being able to “override defs” in such a way, without wrapping, would only be possible in monomorphic code where compiler could always know the exact runtime and compile time type, and only in the code that already know this type. That eliminates:

- all collections
- all tuples
- all functions/lambdas
- all other generic utilities from std (e.g. Option/Either/Try) and 3rd party libraries (e.g. NonEmpty\* from Cats)

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jasper-m/32/1192_2.png) [@Jasper-M](https://contributors.scala-lang.org/u/Jasper-M)\
**Post date:** [November 18, 2026, 12:50pm UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574/13 "2026-11-18T12:50:18Z")

</div>

I wonder in which use case the sweet spot is that having a bare `Array` is required over an `ArraySeq`, but it’s also required to have an immutable view on that `Array` via `IArray` and all the quirks and gotchas of `IArray` don’t matter.

> [@irfanstract](#):
>
> some [tried to migrate the compiler from `List` to `IArray` in #26391](https://github.com/scala/scala3/pull/26391), but later on switched to _value classes_ (`class Lst[+A](_: IArray[?<: A]) extends AnyVal`) instead. best of both worlds.

From what I can see the actual implementation in that PR uses a regular `Array` of `Object`.

```scala
class Lst[+T](private val arr: Array[Object]) extends AnyVal 

```

---

<div class="post-metadata">

**Author:** ![RichType](https://contributors.scala-lang.org/letter_avatar_proxy/v4/letter/r/f14d63/32.png) [@RichType](https://contributors.scala-lang.org/u/RichType)\
**Post date:** [November 19, 2026, 2:52pm UTC](https://contributors.scala-lang.org/t/slc-improve-pattern-matching-on-array-iarray/7574/14 "2026-11-19T14:52:45Z")

</div>

@scalafan You could take a look at my code found here: [openstrat/Util/srcArr](https://github.com/Rich2/openstrat/blob/master/Util/srcArr/RArr.scala) the main driver for this was to create collections for sequences of points, line segments, polygons, grid positions etc. You might notice that the RArr class, essentially an immutable array for reference objects does not extend AnyVal. It used to, and this worked for JVM and JavaScript in Scala 2, however it does not work for Scala.js in Scala 3 and unfortunately the compiler team have said they won’t fix [Avoid bridge clashes involving value classes for anonymous classes](https://github.com/scala/scala3/issues/11264)#11264

So for simplicity I removed the extension from AnyVal so as Js and Jvm could share the same file, and its in the browser that you are most resource limited. The code in that directory uses builders allowing you to map to and between these immutable sequence collections. But there’s also builders for mapping to and between polygons and linepaths in different geometries in the Geom module.

I also found that ArraySeq and Opaque types were not really useful.
