# Bad IArray opaque type interop

**URL:** <https://contributors.scala-lang.org/t/bad-iarray-opaque-type-interop/7485>\
**Category:** Scala Standard Library\
**Created:** [June 21, 2026, 10:55pm UTC](https://contributors.scala-lang.org/t/bad-iarray-opaque-type-interop/7485 "2026-06-21T22:55:01Z")\
**Posts on this page:** 10\
**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:** [June 21, 2026, 10:55pm UTC](https://contributors.scala-lang.org/t/bad-iarray-opaque-type-interop/7485/1 "2026-06-21T22:55:01Z")

</div>

This code fails to compile:

```scala
object Holder {
  opaque type Id <: Int = Int
  def apply(a: Int): Id = a
}

val arr: IArray[Holder.Id] = IArray.tabulate(10)(Holder.apply)
val x: Holder.Id = arr(1) // this line does not compile

```

The error is:

```auto
Found: Int
Required: worksheet.Holder.Id

```

The code starts working if I change `IArray` back to normal `Array`.

---

<div class="post-metadata">

**Author:** ![spamegg1](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/spamegg1/32/2808_2.png) [@spamegg1](https://contributors.scala-lang.org/u/spamegg1)\
**Post date:** [June 22, 2026, 5:08am UTC](https://contributors.scala-lang.org/t/bad-iarray-opaque-type-interop/7485/2 "2026-06-22T05:08:03Z")

</div>

Strangely it also works with `arr.head`. Something about `arr(1)` access…

---

<div class="post-metadata">

**Author:** ![Emil](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/emil/32/3398_2.png) [@Emil](https://contributors.scala-lang.org/u/Emil)\
**Post date:** [June 22, 2026, 5:50am UTC](https://contributors.scala-lang.org/t/bad-iarray-opaque-type-interop/7485/3 "2026-06-22T05:50:31Z")

</div>

I probably caused by the fact IArray is overloaded: [scala3/library/src/scala/IArray.scala at d61508a90a9edda1f8c060d72ed327e58e099537 · eejbyfeldt/scala3 · GitHub](https://github.com/eejbyfeldt/scala3/blob/d61508a90a9edda1f8c060d72ed327e58e099537/library/src/scala/IArray.scala#L23-L31)

And the bound `Id <: Int` means that `IArray[Holder.Id] <:< IArray[Int]` and therefore the `Int` overload is picked.

One work around could be to add another version of apply in the Holder object with will be chosen e.g

```auto
  extension (arr: IArray[Id]) def apply(n: Int): Id = arr.apply(n)

```

---

<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:** [June 22, 2026, 11:44am UTC](https://contributors.scala-lang.org/t/bad-iarray-opaque-type-interop/7485/4 "2026-06-22T11:44:25Z")

</div>

A better workaround is re-wrapping in Id.apply after array access.

But this is not about finding a wokraround, fixing the root cause is preferred.

Why is apply overloaded, but not head and other methods?

---

<div class="post-metadata">

**Author:** ![Emil](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/emil/32/3398_2.png) [@Emil](https://contributors.scala-lang.org/u/Emil)\
**Post date:** [June 22, 2026, 12:41pm UTC](https://contributors.scala-lang.org/t/bad-iarray-opaque-type-interop/7485/5 "2026-06-22T12:41:00Z")

</div>

I looked at the git history and it seems like the overloads where add in [Disallow inline methods and opaque types in same scope by odersky · Pull Request #6853 · scala/scala3 · GitHub](https://github.com/scala/scala3/pull/6853)  
and the implementation restriction was later removed in  
[Drop "no inlines with opaques" implementation restriction by odersky · Pull Request #12815 · scala/scala3 · GitHub](https://github.com/scala/scala3/pull/12815)

---

<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:** [June 25, 2026, 9:43am UTC](https://contributors.scala-lang.org/t/bad-iarray-opaque-type-interop/7485/6 "2026-06-25T09:43:11Z")

</div>

`toSet` also does not play well (but it does with normal `Array`). So in current form, `IArray` is unusable. Too many quirks.

```scala
object Holder {
  opaque type Id <: Int = Int
  def apply(a: Int): Id = a
}

val arr: IArray[Holder.Id] = IArray.tabulate(10)(Holder.apply)
val x: Set[Holder.Id] = arr.toSet // does not compile

```

---

<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:** [June 25, 2026, 10:08am UTC](https://contributors.scala-lang.org/t/bad-iarray-opaque-type-interop/7485/7 "2026-06-25T10:08:29Z")

</div>

you can fix this with `opaque type Id >: Int <: Int = Int`,

what’s happening is that because `Array` is invariant, then `Holder.Id` is not exactly `Int` so it wraps in `Predef.genericWrapArray`.

because `IArray` is covariant, then `IArray[Holder.Id]` counts as `IArray[Int]` so applies `IArray.wrapIntArray`, and `ArraySeq.ofInt` requires a lower bound of Int for the type argument to `toSet`

```scala
object Holder {
  opaque type Id >: Int <: Int = Int
  def apply(a: Int): Id = a
}

val arr: IArray[Holder.Id] = IArray.tabulate(10)(Holder.apply)
val x: Set[Holder.Id] = arr.toSet // ok

```

Perhaps we should look into downcasting as an improvement, so the conversion will convert `IArray[Holder.Id]` to `(new ArraySeq.OfInt(...)).asInstanceOf[ArraySeq[Holder.Id]]`.

something like:

```scala
implicit def wrapIntArrayV2[T <: Int](arr: IArray[T]): ArraySeq[T] =
  mapNull(arr, new ArraySeq.ofInt(arr.asInstanceOf[Array[Int]]).asInstanceOf[ArraySeq[T]])

```

---

<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:** [June 26, 2026, 3:37pm UTC](https://contributors.scala-lang.org/t/bad-iarray-opaque-type-interop/7485/8 "2026-06-26T15:37:08Z")

</div>

a slight wrinkle - there could be code out there relying on the result of conversion being exactly `ArraySeq.ofInt`, but we could bridge that by keeping the present conversions as non-implicit

---

<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:** [June 28, 2026, 9:41pm UTC](https://contributors.scala-lang.org/t/bad-iarray-opaque-type-interop/7485/9 "2026-06-28T21:41:27Z")

</div>

> [@bishabosha](#):
>
> `opaque type Id >: Int <: Int = Int`

I appreciate the suggestion, but defining the type like this defeats the purpose of an opaque type, making `Id` always typecheck equivalently to `Int`.

For example into `f(id: Holder.Id)` you can pass `f(10)` and it will typecheck, therefore bypassing the safeguards in `Id.apply()`

Why cannot `IArray` have the same behavior as a normal `Array`?

---

<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:** [June 29, 2026, 9:18am UTC](https://contributors.scala-lang.org/t/bad-iarray-opaque-type-interop/7485/10 "2026-06-29T09:18:56Z")

</div>

> [@scalafan](#):
>
> Why cannot `IArray` have the same behavior as a normal `Array`?

its covariant (i.e. `IArray[+T]` ) vs `Array[T]`.

I guess the choice could have been made to keep it invariant, but that can’t change now.

I would basically make the specialised conversions non-implicit so then it will always use the generic conversion (with generic return type), perhaps we can still shortcut the runtime type test within the conversion method.
