# InlineArray pattern matching and destructuring

**URL:** <https://forums.swift.org/t/inlinearray-pattern-matching-and-destructuring/84032>\
**Category:** Discussion\
**Tags:** pattern-matching, inlinearray\
**Created:** [January 9, 2026, 6:13am UTC](https://forums.swift.org/t/inlinearray-pattern-matching-and-destructuring/84032 "2026-01-09T06:13:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lancelotbronner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancelotbronner/32/22071_2.png) [@Lancelotbronner](https://forums.swift.org/u/Lancelotbronner)\
**Post date:** [January 9, 2026, 6:13am UTC](https://forums.swift.org/t/inlinearray-pattern-matching-and-destructuring/84032/1 "2026-01-09T06:13:12Z")

</div>

I have come across a need in pattern matching an `InlineArray` and was very surprised to find out that while I can do this:

```swift
@available(macOS 26, *)
mutating func names<let count: Int>() -> [count of Name] {
	InlineArray { _ in _names.next() }
}

```

I can't actually use this the way I wanted:

```swift
// doesn't work :(
let [x, y, z] = names()

```

forcing me to instead do either of these, which are suboptimal because I'm going to be writing hundreds of tests with that pattern:

```swift
// locals[0], locals[1], etc. I've lost the name :(
let locals = names()
// got the name back but "lost" my short-hand :(
let (x, y, z) = (locals[0], locals[1], locals[2])
// wonderful! except...
let (x, y, z) = names()
// ...now I've got this in my code and it keeps growing to like 30
	mutating func vars() -> (Var) {
		(_vars.next())
	}

	mutating func vars() -> (Var, Var) {
		(_vars.next(), _vars.next())
	}

	mutating func vars() -> (Var, Var, Var) {
		(_vars.next(), _vars.next(), _vars.next())
	}

	mutating func vars() -> (Var, Var, Var, Var) {
		(_vars.next(), _vars.next(), _vars.next(), _vars.next())
	}

	mutating func vars() -> (Var, Var, Var, Var, Var) {
		(_vars.next(), _vars.next(), _vars.next(), _vars.next(), _vars.next())
	}

	mutating func vars() -> (Var, Var, Var, Var, Var, Var) {
		(_vars.next(), _vars.next(), _vars.next(), _vars.next(), _vars.next(), _vars.next())
	}

	mutating func vars() -> (Var, Var, Var, Var, Var, Var, Var) {
		(_vars.next(), _vars.next(), _vars.next(), _vars.next(), _vars.next(), _vars.next(), _vars.next())
	}

	mutating func vars() -> (Var, Var, Var, Var, Var, Var, Var, Var) {
		(_vars.next(), _vars.next(), _vars.next(), _vars.next(), _vars.next(), _vars.next(), _vars.next(), _vars.next())
	}

```

I'd therefore like to suggest an `InlineArray` pattern matching.

```swift
let [x, y, z] = names()

switch names {
case let [x]: // ...
case let [x, y]: // ...
case let [x, y, z]: // ...
case let [_, y, z, w] where y * z == w: // ...
default: // ...
}

if case let [x] = names { /* ... */ }

```

and so forth.

Thoughts?

---

<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:** [January 9, 2026, 3:53pm UTC](https://forums.swift.org/t/inlinearray-pattern-matching-and-destructuring/84032/2 "2026-01-09T15:53:28Z")

</div>

Why don't you use a tuple instead?

---

<div class="post-metadata">

**Author:** ![Lancelotbronner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancelotbronner/32/22071_2.png) [@Lancelotbronner](https://forums.swift.org/u/Lancelotbronner)\
**Post date:** [January 9, 2026, 6:47pm UTC](https://forums.swift.org/t/inlinearray-pattern-matching-and-destructuring/84032/3 "2026-01-09T18:47:29Z")

</div>

Because then I have to make a names function for each possible arity, that’s what I’m doing right now but I have like 30 functions for returning tuples of 1-30 elements, I’d much rather be able to write 1 function that infers the count!

---

<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:** [January 9, 2026, 6:50pm UTC](https://forums.swift.org/t/inlinearray-pattern-matching-and-destructuring/84032/4 "2026-01-09T18:50:42Z")

</div>

> [@Lancelotbronner](#):
>
> Because then I have to make a names function for each possible arity, that’s what I’m doing right now but I have like 30 functions for returning tuples of 1-30 elements, I’d much rather be able to write 1 function that infers the count!

Can you explain your actual use-case in more detail? It's hard to imagine a situation where this would be meaningful:

```swift
let (x1, x2, x3, /* ... */, x30) = names()

... expression involving all of x1 ... x30

```

---

<div class="post-metadata">

**Author:** ![Lancelotbronner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancelotbronner/32/22071_2.png) [@Lancelotbronner](https://forums.swift.org/u/Lancelotbronner)\
**Post date:** [January 9, 2026, 6:54pm UTC](https://forums.swift.org/t/inlinearray-pattern-matching-and-destructuring/84032/5 "2026-01-09T18:54:16Z")

</div>

Unit tests for various passes of a toy compiler. I’m naming all those variables and building up the input and expected structure using them.

Also in unit tests for graphs where I name nodes to build up graphs.

Basically at this scale, unit tests!

---

<div class="post-metadata">

**Author:** ![Lancelotbronner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancelotbronner/32/22071_2.png) [@Lancelotbronner](https://forums.swift.org/u/Lancelotbronner)\
**Post date:** [January 9, 2026, 6:57pm UTC](https://forums.swift.org/t/inlinearray-pattern-matching-and-destructuring/84032/6 "2026-01-09T18:57:04Z")

</div>

Another option could be supporting same-type constraints on parameter packs, that would also support my use case.

---

<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:** [January 9, 2026, 7:11pm UTC](https://forums.swift.org/t/inlinearray-pattern-matching-and-destructuring/84032/7 "2026-01-09T19:11:30Z")

</div>

> [@Lancelotbronner](#):
>
> Unit tests for various passes of a toy compiler. I’m naming all those variables and building up the input and expected structure using them.
> 
> Also in unit tests for graphs where I name nodes to build up graphs.

You could forgo type safety and just use strings to name your nodes, or you could make your graph data type generic over a `NameType: Hashable` that is used for the names, and then define local enums like this:

```swift
enum Name {
  case x
  case y
  case z
}

```

---

<div class="post-metadata">

**Author:** ![Lancelotbronner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancelotbronner/32/22071_2.png) [@Lancelotbronner](https://forums.swift.org/u/Lancelotbronner)\
**Post date:** [January 9, 2026, 7:14pm UTC](https://forums.swift.org/t/inlinearray-pattern-matching-and-destructuring/84032/8 "2026-01-09T19:14:55Z")

</div>

That could be an option, it would require large adjustments to the main code just for the tests which isn’t ideal but it would definitely have its advantages!

---

<div class="post-metadata">

**Author:** ![Lancelotbronner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancelotbronner/32/22071_2.png) [@Lancelotbronner](https://forums.swift.org/u/Lancelotbronner)\
**Post date:** [January 12, 2026, 11:18pm UTC](https://forums.swift.org/t/inlinearray-pattern-matching-and-destructuring/84032/10 "2026-01-12T23:18:10Z")

</div>

While I love this hacky solution, its still longer at call-site, I’ll keep my 30 functions for now!

---

<div class="post-metadata">

**Author:** ![Lancelotbronner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancelotbronner/32/22071_2.png) [@Lancelotbronner](https://forums.swift.org/u/Lancelotbronner)\
**Post date:** [June 21, 2026, 10:14am UTC](https://forums.swift.org/t/inlinearray-pattern-matching-and-destructuring/84032/11 "2026-06-21T10:14:00Z")

</div>

I have once again encountered this issue, this time with decompilers.  
Here's what I'd like to be able to do:

```swift
func stride<let count: Int>(
	from offset: Offset,
	by stride: Offset,
	in space: AddressSpaceId,
	size: UInt32
) -> InlineArray<count, Varnode> {
	InlineArray(first: Varnode(at: offset, in: space, size: size)) { previous in
		previous.advanced(by: stride)
	}
}

let [RAX, RCX, RDX, RBX, RSP, RBP, RSI, RDI] = Varnode.stride(from: 0, by: 8, in: .reg, size: 8)
let [EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI] = Varnode.stride(from: 0, by: 8, in: .reg, size: 4)
let [AX, CX, DX, BX, SP, BP, SI, DI] = Varnode.stride(from: 0, by: 8, in: .reg, size: 2)
let [AL, CL, DL, BL, SPL, BPP, SIL, DIL] = Varnode.stride(from: 0, by: 8, in: .reg, size: 1)
let [AH, CH, DH, BH] = Varnode.stride(from: 1, by: 8, in: .reg, size: 1)

```

Here's what I'm forced to do:

```swift
let (RAX, RCX, RDX, RBX, RSP, RBP, RSI, RDI) = Varnode.stride(from: 0, by: 8, in: .reg, size: 8)
let (EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI) = Varnode.stride(from: 0, by: 8, in: .reg, size: 4)
let (AX, CX, DX, BX, SP, BP, SI, DI) = Varnode.stride(from: 0, by: 8, in: .reg, size: 2)
let (AL, CL, DL, BL, SPL, BPP, SIL, DIL) = Varnode.stride(from: 0, by: 8, in: .reg, size: 1)
let (AH, CH, DH, BH) = Varnode.stride(from: 1, by: 8, in: .reg, size: 1)

// then the functions, up to 32

func stride(
	from offset: Offset,
	by stride: Offset,
	in space: AddressSpaceId,
	size: UInt32
) -> (Varnode) {
	let names: [1 of Varnode] = Self.stride(from: offset, by: stride, in: space, size: size)
	return (names[0])
}

func stride(
	from offset: Offset,
	by stride: Offset,
	in space: AddressSpaceId,
	size: UInt32
) -> (Varnode, Varnode) {
	let names: [2 of Varnode] = Self.stride(from: offset, by: stride, in: space, size: size)
	return (names[0], names[1])
}

func stride(
	from offset: Offset,
	by stride: Offset,
	in space: AddressSpaceId,
	size: UInt32
) -> (Varnode, Varnode, Varnode) {
	let names: [3 of Varnode] = Self.stride(from: offset, by: stride, in: space, size: size)
	return (names[0], names[1], names[2])
}

// on and on...

```
