Using `indirect` modifier for `struct` properties

Yes, and thus by a property wrapper:

@propertyWrapper
enum Indirect<T> {
  indirect case wrapped(T)
  
  var wrappedValue: T {
    get { switch self { case .wrapped(let x): return x } }
    set { self = .wrapped(newValue) }
  }
}

Usage:

struct Node<T> {
  var value: T
  @Indirect var next: Self?
  
  init(_ value: T, next: Self? = nil) {
    self.value = value
    self._next = .wrapped(next)
  }
}
19 Likes