# Creating a subscript for each element of a parameter pack

**URL:** <https://forums.swift.org/t/creating-a-subscript-for-each-element-of-a-parameter-pack/83593>\
**Category:** Using Swift\
**Tags:** parameter-packs\
**Created:** [December 7, 2025, 4:56am UTC](https://forums.swift.org/t/creating-a-subscript-for-each-element-of-a-parameter-pack/83593 "2025-12-07T04:56:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [December 7, 2025, 4:56am UTC](https://forums.swift.org/t/creating-a-subscript-for-each-element-of-a-parameter-pack/83593/1 "2025-12-07T04:56:50Z")

</div>

I want to use parameter packs to provide a type-safe query API. But I can’t figure out how my query type’s generic subscript can enforce that its type parameter is in the parameter pack that was passed to the type’s type parameter. Here’s my declaration so far:

```swift
struct Query<ElementTypes: each Element> {
  public subscript<E>(_: E.Type = E.self) -> E { }
}

```

I want to tell the compiler that `E` must be an element of `ElementTypes`. Alternatively, I could make the subscript non-generic if I could tell the compiler that for each element `X` of `ElementTypes`, there exists a `public subscript(dummy: X.Type = X.self) -> X`.

Is either of these possible express in Swift today?

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [December 7, 2025, 4:18pm UTC](https://forums.swift.org/t/creating-a-subscript-for-each-element-of-a-parameter-pack/83593/3 "2025-12-07T16:18:11Z")

</div>

> [@ksluder](#):
>
> Is either of these possible express in Swift today?

A good way to think about "new" kinds of requirements is to consider what happens if you observe it from a different generic context. For example, if you have this, it's not so bad, you'd just have to re-state your "is an element of pack" requirement relating `E` with `each T` on f():

```swift
func f<each T, E>(..., e: E) {
  let q: Query<repeat each T>(...)

  let value: E = q[]
}

```

However, now let's suppose we use your `Query` type like this:

```swift
func f<each T, each U, E>(..., e: E) {
  let q: Query<repeat each T, repeat each U>(...)

  let value: E = q[]
}

```

This time, you'd have to statically declare that `E` is an element of `each T`, _or_ `each U`, so suddenly you need to introduce "disjunction requirements" in your generics system to be able to use this in full generality. Or what about this:

```swift
func f<each T, E>(..., e: E) {
  let q: Query<repeat each T, Int>(...)

  let value: E = q[]
}

```

Now the subscript is valid only if `E == Int` (same-type requirement), _or_ `E` is an element of `each T`, so now you've got to be able to deal with disjunction requirements that contain arbitrary other requirements, not just your new kind.

So I suspect the original idea of a "is an element of pack" requirement is probably unworkable, because it doesn't generalize very well.

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [December 7, 2025, 5:06pm UTC](https://forums.swift.org/t/creating-a-subscript-for-each-element-of-a-parameter-pack/83593/4 "2025-12-07T17:06:57Z")

</div>

I believe the `E in Elements` version implies the ability to treat _any_ type sequence (aka generic parameter pack) as a disjunction. It doesn’t just arise in certain special cases.

What about the alternative of declaring that for each element `X` of `Elements`, there exists a `subscript` specialized for that `X`? I don’t think that would risk introducing problems, since I could write the same thing out by hand. And it would achieve my original goal of type-safe access to the fields in a query result.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [December 7, 2025, 5:40pm UTC](https://forums.swift.org/t/creating-a-subscript-for-each-element-of-a-parameter-pack/83593/5 "2025-12-07T17:40:02Z")

</div>

> [@ksluder](#):
>
> What about the alternative of declaring that for each element `X` of `Elements`, there exists a `subscript` specialized for that `X`?

This would require a new form of dynamic dispatch if you used your subscript from a generic context. And if you’re not using your subscript from a generic context, such a declaration wouldn’t really do anything.

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [December 7, 2025, 8:03pm UTC](https://forums.swift.org/t/creating-a-subscript-for-each-element-of-a-parameter-pack/83593/7 "2025-12-07T20:03:47Z")

</div>

I do think there is a benefit in a non-generic context. My broader goal is to create a `Transaction` protocol that specifies what `Elements` it reads from and writes to. This would enable my program to automatically determine transaction dependencies and parallelize those without conflicting dependencies. The reason for doing this in the type system is so that the `Transaction` conforming type cannot accidentally read from or write to `Element`s it has not statically declared.
