# New keypath subscript

**URL:** <https://forums.swift.org/t/new-keypath-subscript/65495>\
**Category:** Pitches\
**Tags:** keypaths, subscripts\
**Created:** [June 11, 2023, 1:26am UTC](https://forums.swift.org/t/new-keypath-subscript/65495 "2023-06-11T01:26:01Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [June 11, 2023, 1:26am UTC](https://forums.swift.org/t/new-keypath-subscript/65495/1 "2023-06-11T01:26:01Z")

</div>

If I override:

```
subscript<T>(keyPath keyPath: WritableKeyPath<Self, T>) -> T { ... }

```

it won't be called when I'm getting/setting fields directly.

Could we have another subscript in addition, say:

```
subscript<T>(proxy keyPath: WritableKeyPath<Self, T>) -> T { ... }

```

which if specified would be called when I'm getting / setting fields directly?

```swift
struct S {
    var x = 1, y = 2
    
    subscript<T>(proxy keyPath: WritableKeyPath<Self, T>) -> T {
        get {
            // call though default implementation:
            let result = self[keyPath: keyPath]
            print("get \(keyPath) -> \(result)")
            return result
        }
        set {
            print("set \(keyPath) = \(newValue)")
            // call though default implementation:
            self[keyPath: keyPath] = newValue
        }
    }
}

var s = S()
s.x = 42 // set \S.x = 42
print(s.x) // get \S.x -> 42

```

---

<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:** [June 15, 2023, 11:04am UTC](https://forums.swift.org/t/new-keypath-subscript/65495/2 "2023-06-15T11:04:21Z")

</div>

This new "proxy" (bikeshed name) subscript will fill the gap between "keyPath" subscript and "dynamicMember" subscript.

I guess I could use this approach instead:

```swift
class S {
    var x: Int {
        get {
            print("get \(\S.x) -> \(_x)")
            return _x
        }
        set {
            _x = newValue
            print("set \(\S.x) = \(newValue)")
        }
    }
    var _x: Int = 1
    
    var y: Int {
        get {
            print("get \(\S.y) -> \(_y)")
            return _y
        }
        set {
            _y = newValue
            print("set \(\S.y) = \(newValue)")
        }
    }
    var _y: Int = 2
    // and so on and so forth
}

```

or the equivalent code generated by macro, similar to how new Observation machinery is doing it.

I wonder though if the keypath subscript based approach is superior; IMHO it feels lighter and more elegant.

---

<div class="post-metadata">

**Author:** ![aggie33](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aggie33/32/29896_2.png) [@aggie33](https://forums.swift.org/u/aggie33)\
**Post date:** [June 16, 2023, 2:16pm UTC](https://forums.swift.org/t/new-keypath-subscript/65495/3 "2023-06-16T14:16:22Z")

</div>

You could do something like this:

```swift
@dynamicMemberLookup public struct S {
  public struct Storage {
    var x: Int
    var y: Int
  }

  private var storage: Storage

  public subscript<T>(dynamicMember member: WritableKeyPath<Storage, T>) {
    get { storage[keyPath: member] }
    set { storage[keyPath: member] = newValue }
  }
}
```
