# Indirect Members

**URL:** <https://forums.swift.org/t/indirect-members/81129>\
**Category:** Discussion\
**Created:** [July 15, 2025, 2:59pm UTC](https://forums.swift.org/t/indirect-members/81129 "2025-07-15T14:59:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anachron](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@Anachron](https://forums.swift.org/u/Anachron)\
**Post date:** [July 15, 2025, 2:59pm UTC](https://forums.swift.org/t/indirect-members/81129/1 "2025-07-15T14:59:29Z")

</div>

Hello community,

With enums, you can have indirect cases:

```swift
enum Tree<T> {
   case Leaf(T)
   indirect case Node(Tree, Tree)
}

```

But with structs, last time I checked, you can not do

```swift
struct LinkedList<T> {
   var value : T
   indirect var tail : LinkedList<T>?
}

```

This may be tolerable for internal types, but what if you interface with a REST API from another company and some API call can return a type where the json schema is recursive? Do I just have to switch to reference types?

Maybe allowing to annotate optional struct members as indirect would be a cleaner solution in the future.

---

<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:** [July 15, 2025, 4:01pm UTC](https://forums.swift.org/t/indirect-members/81129/2 "2025-07-15T16:01:10Z")

</div>

> [@Anachron](#):
>
> ```swift
> struct LinkedList<T> {
> var value : T
> indirect var tail : LinkedList<T>?
> }
> 
> ```

How about this:

```swift
struct LinkedList<T> {
  var value: T
  var _tail: [LinkedList<T>]
  var tail: LinkedList<T>? {
    get { return _tail.first }
    set { _tail = [newValue] }
  }
}

```

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [July 15, 2025, 4:13pm UTC](https://forums.swift.org/t/indirect-members/81129/3 "2025-07-15T16:13:52Z")

</div>

The naive implementation of this would have to allocate for every new value, FWIW. Presumably you’re imagining `Optional<Box<LinkedList>>`, but it would actually be `Box<Optional<LinkedList>>` unless we made an exception for optionals.

---

<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:** [July 15, 2025, 6:29pm UTC](https://forums.swift.org/t/indirect-members/81129/4 "2025-07-15T18:29:01Z")

</div>

Maybe Swift should define a (back-ported) `Box` type in the standard library so the ecosystem could use the same one.

```swift
public final class Box<T> { 
    public var wrappedValue: T { get set }
    public init(wrappedValue: T)
}

```

Then you can define your linked list as

```swift
struct LinkedList<T> {
   var value : T
   var tail : Box<LinkedList<T>>?
}

```

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [July 15, 2025, 7:01pm UTC](https://forums.swift.org/t/indirect-members/81129/5 "2025-07-15T19:01:37Z")

</div>

You may be interested in this pitch for the same feature:

> [@Using \`indirect\` modifier for \`struct\` properties](https://forums.swift.org/t/using-indirect-modifier-for-struct-properties/37600):
>
> It is a known limitation that structs, being value types, cannot contain properties of the same type. For example, the following code is invalid: struct Foo { var foo: Foo // Error: Value type 'Foo' cannot have a stored property that recursively contains it } This code is invalid because in order to create an instance of Foo, we would have to provide an infinite amount of instances. Instantiating it would require infinite memory. Furthermore, the following code is also invalid: struct List\<…

---

<div class="post-metadata">

**Author:** ![Anachron](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@Anachron](https://forums.swift.org/u/Anachron)\
**Post date:** [July 15, 2025, 7:05pm UTC](https://forums.swift.org/t/indirect-members/81129/6 "2025-07-15T19:05:38Z")

</div>

ahhhh thanks, the preview didn't recommend this one as "similar" to me

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [July 17, 2025, 7:14pm UTC](https://forums.swift.org/t/indirect-members/81129/7 "2025-07-17T19:14:58Z")

</div>

Would be logical to have it available out of the box for structs.

For the time being you could use a simple workaround:

```swift
struct Node {
    let payload: Int
    let next: IndirectOptional<Node>
}

```

where:

```swift
indirect enum IndirectOptional<T> {
    case none
    case some(T)
}

```

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [July 17, 2025, 7:24pm UTC](https://forums.swift.org/t/indirect-members/81129/8 "2025-07-17T19:24:15Z")

</div>

The problem with the "wrap the property type in a box" approach is that it forces a separate heap allocation for _each_ indirect property, which is going to increase memory fragmentation. This is fine if you just have a single property, but if you start adding more indirect properties, you're going to want them to be contained in the same "box".

Someone could probably develop a macro-based solution here that would extract the relevant properties into a storage class, but this is something that the language should really support directly because there are subtle inefficiencies/feetguns if you do it wrong (or even do it right): you have to implement copy-on-write yourself if your type is mutable, you have to write forwarding accessors and make sure they don't become a performance bottleneck, the box class adds useless metadata to the binary that you don't actually need/want, etc.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [October 6, 2026, 3:17pm UTC](https://forums.swift.org/t/indirect-members/81129/9 "2026-10-06T15:17:11Z")

</div>

It's possible to [use a protocol as a workaround](https://forums.swift.org/t/unorthodox-recursive-data-structures) for indirect struct fields. Same could be used for enums instead of using the built-in `indirect` (just the latter is nicer and does not require an ad hoc protocol).
