# Why can’t key paths refer to instance methods?

**URL:** <https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315>\
**Category:** Discussion\
**Tags:** keypaths, functions\
**Created:** [April 12, 2020, 6:23pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315 "2020-04-12T18:23:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [April 12, 2020, 6:23pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/1 "2020-04-12T18:23:27Z")

</div>

In most parts of Swift, instance methods are treated like properties. However, if you try to reference an instance method using a key path, you get a compiler error. Why is this the case? I assume there is a reason, but I cannot find it. It would be extremely useful in all the same places that other key paths are.

Note that I am not asking about _calling_ instance methods inside a key path. I want to know why this works:

```swift
let instance = 100
let method = instance.isMultiple

method(10) // true
method(11) // false

```

while this doesn’t:

```swift
let instance = 100
let method = instance[keyPath: \.isMultiple] // Key path cannot refer to instance method 'isMultiple(of:)'

method(10)
method(11)

```

---

<div class="post-metadata">

**Author:** ![filip-sakel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/filip-sakel/32/8697_2.png) [@filip-sakel](https://forums.swift.org/u/filip-sakel)\
**Post date:** [April 12, 2020, 6:34pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/2 "2020-04-12T18:34:44Z")

</div>

I was wondering about that too. I firmly believe that key paths referring to instance methods would be a really powerful feature and seems only natural to be added to the language. I thought that the following would work normally:

```swift
struct Dog: Animal {
    var age: Int
    
    func greet() { /*...*/ }
    
    func bark() { /*...*/ }
}

@dynamicMemberLookup
struct AnimalWrapper<Wrapped: Animal> {
    var wrapped: Wrapped
    
    subscript<Value>(dynamicMember keyPath: KeyPath<Wrapped, Value>) -> Value {
        /* ... */
    }
}

let dog = Dog(age: 13)
let animal = AnimalWrapper(wrapped: dog)

animal.age // Int
animal.greet // () -> ()

```

This doesn't work because keyPaths can't reference instance methods, but getting the bark method as a closure is allowed:

```swift
let bark = Dog(age: 5).bark // () -> ()

```

---

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [April 12, 2020, 6:37pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/3 "2020-04-12T18:37:50Z")

</div>

That’s precisely the sort of thing I want to do. Dynamic member lookup with key paths would be perfect for wrapper types, were it not for this limitation.

---

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [April 12, 2020, 11:55pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/4 "2020-04-12T23:55:44Z")

</div>

If you assign an instance method to a property, that property can be referenced through key paths. I know that a function’s type does not include parameter labels: maybe it’s that sort of label erasure that requires special support?

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [April 13, 2020, 4:27am UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/5 "2020-04-13T04:27:44Z")

</div>

> [@Saklad5](#):
>
> Why is this the case?

It‘s simple, no one has implemented it yet. ;)

---

<div class="post-metadata">

**Author:** ![filip-sakel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/filip-sakel/32/8697_2.png) [@filip-sakel](https://forums.swift.org/u/filip-sakel)\
**Post date:** [April 13, 2020, 9:44am UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/6 "2020-04-13T09:44:34Z")

</div>

I'm not at all familiar with compiler architecture and the process of contribution. Do you happen to know what would need to be done in order to implement this feature?

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [April 13, 2020, 9:47am UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/7 "2020-04-13T09:47:29Z")

</div>

I'm not a compiler developer either. Therefore I have no real clue.

I guess you could start in this file and try to understand the important pieces. [swift/KeyPath.swift at main · apple/swift · GitHub](https://github.com/apple/swift/blob/master/stdlib/public/core/KeyPath.swift)

---

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [April 13, 2020, 12:39pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/8 "2020-04-13T12:39:09Z")

</div>

> <https://github.com/apple/swift/blob/2fa4fbb7a9aaf166f06b73e20ac17b4686d47699/test/attr/attr_dynamic_member_lookup.swift#L672-L673>

I found the precise limitations.

---

<div class="post-metadata">

**Author:** ![Jean-Daniel](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@Jean-Daniel](https://forums.swift.org/u/Jean-Daniel)\
**Post date:** [April 13, 2020, 1:12pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/9 "2020-04-13T13:12:19Z")

</div>

This snippet explains the limitation of dynamic member lookup, not KeyPath. This is not an explanation of why KeyPaths don't supports method, but a consequence.

---

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [April 13, 2020, 1:12pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/10 "2020-04-13T13:12:59Z")

</div>

They _are_ the limitations, though. I’m not saying they’re the source of them.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [April 13, 2020, 3:42pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/11 "2020-04-13T15:42:52Z")

</div>

There's no fundamental reason KeyPaths can't support instance methods. It would be a reasonable thing to add to the language. The implementation would be more or less identical to how read-only subscripts are handled.

---

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [April 13, 2020, 3:53pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/12 "2020-04-13T15:53:03Z")

</div>

What about static members? Is that just because of ambiguity?

I haven’t contributed to Swift before, but I might look into doing so for methods. With a formal pitch, of course.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [April 13, 2020, 3:53pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/13 "2020-04-13T15:53:59Z")

</div>

Static members could be made to work too. I think @beccadax had taken a stab at it a while back, but I don't recall what limits he ran into.

---

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [April 13, 2020, 4:01pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/14 "2020-04-13T16:01:08Z")

</div>

This appears to be the stab in question: [GitHub - beccadax/swift at i-am-the-keymaster](https://github.com/brentdax/swift/tree/i-am-the-keymaster)

Judging by the last commit on the branch, his implementation wasn’t compatible with Swift 5.0 (the contemporary version). It worked on the Swift 5.1 beta, though.

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [April 13, 2020, 4:09pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/15 "2020-04-13T16:09:14Z")

</div>

This would be quite fantastic to get going, and we've been dreaming up some use-cases for this a while ago.

What I would be most interested in is ensuring that it is also possible to capture arguments in a KeyPath (slowly leading towards not only wrapping properties, but any function really via a kind of "function wrapper" which would kind of look the same -- get a KeyPath representing the invocation)...

---

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [April 13, 2020, 4:13pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/16 "2020-04-13T16:13:06Z")

</div>

Assuming you mean argument labels, that seems _way_ out of scope for this. I agree that it would be helpful, but that isn’t actually part of a function’s type right now.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [April 13, 2020, 4:14pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/17 "2020-04-13T16:14:13Z")

</div>

Key paths to methods would handle arguments the same way subscript key paths handle indexes, by capturing them into the KeyPath object.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [April 13, 2020, 7:27pm UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/18 "2020-04-13T19:27:33Z")

</div>

I still hope that we could somehow workaround the requirement of every parameter being hashable for key-path methods.

---

<div class="post-metadata">

**Author:** ![Paul\_Cantrell](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paul_cantrell/32/16764_2.png) [@Paul\_Cantrell](https://forums.swift.org/u/Paul_Cantrell)\
**Post date:** [April 14, 2020, 12:57am UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/19 "2020-04-14T00:57:46Z")

</div>

This one is [on my wish list](https://gist.github.com/pcantrell/208f26bdbe21b1134309cc7706dda5cb), because [Siesta](https://bustoutsolutions.github.io/siesta/) has a compelling use case for it. A common mistake when using Siesta is to do this:

```
resource.addObserver(owner: self, observer: self.fooChanged)

```

This innocent code creates a retain cycle under Siesta’s memory rules. No need to understand the context or fiddly details here; the fundamental problem is that `self.fooChanged` retains `self`, and the way Siesta works, we don’t want that.

Folks have suggested weak closures and/or weak method references (e.g. [here](https://forums.swift.org/t/pitch-weak-method-storage-modifiers-aka-weak-references/12161), [here](https://forums.swift.org/t/pitch-introduction-of-weak-unowned-closures/6095)), but another alternative that requires no new syntax and little new discussion — just the proposal at hand here — would be to alter Siesta’s observer API to use a keypath:

```
resource.addObserver(owner: self, observer: \.fooChanged) // keypath doesn’t retain self!

```

Since the idea being pitched in this thread sounds feasible, I’d be happy to help write it up as a formal proposal.

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [April 14, 2020, 1:55am UTC](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/20 "2020-04-14T01:55:57Z")

</div>

> [@Paul\_Cantrell](#):
>
> another alternative that requires no new syntax and little new discussion — just the proposal at hand here — would be to alter Siesta’s observer API to use a keypath:

Did you see @Joe_Groff’s comment:

> [@Why can’t key paths refer to instance methods?](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315/11):
>
> There's no fundamental reason KeyPaths can't support instance methods. It would be a reasonable thing to add to the language. The implementation would be more or less identical to how read-only subscripts are handled.

If methods in key paths work like read-only subscripts, all arguments would need to be bound. Only `self` Is not bound. I looked at the siesta docs and it looks like the callback accepts two arguments that must be provided by the caller and neither appear to be the observer’s self which is what you would expect to pass to a key path.

It would be great if we could extend the read only subscript model somehow to support unbound methods where parameters are unbound _in addition_ to `self`, or perhaps even allowing arbitrary “holes” in the path (including subscript parameters). This works with key path syntax. It might be possible to make it work with the `KeyPath` type as well by making `Root` a tuple, but I’m not sure that’s a good idea.

[Next page](https://forums.swift.org/t/why-can-t-key-paths-refer-to-instance-methods/35315.md?page=2)
