You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -85,27 +85,19 @@ If the member is a metatype (e.g., a static method, class method, initializer, o
85
85
86
86
```swift
87
87
structCalculator {
88
-
funcadd(_a: Int, _b: Int) ->Int {
88
+
staticfuncadd(_a: Int, _b: Int) ->Int {
89
89
return a + b
90
90
}
91
91
}
92
92
93
-
let calc = Calculator.self
94
-
let addKeyPath: KeyPath<Calculator.Type, (Calculator) -> (Int, Int) ->Int> = \Calculator.Type.add
93
+
let addKeyPath: KeyPath<Calculator.Type, Int> = \Calculator.Type.add(4, 5)
95
94
```
96
95
97
-
Here, `addKeyPath` is a key path that references the add method of `Calculator` as a metatype member. The key path’s root type is `Calculator.Type`, and it resolves to an unapplied instance method: `(Calculator) -> (Int, Int) -> Int`. This represents a curried function where the first step binds an instance of `Calculator`, and the second step applies the method arguments.
98
-
99
-
```swift
100
-
let addFunction = calc[keyPath: addKeyPath]
101
-
let fullyApplied =addFunction(Calculator())(20, 30)`
102
-
```
103
-
104
-
`addFunction` applies an instance of Calculator to the key path method. `fullyApplied` further applies the arguments (20, 30) to produce the final result.
96
+
Here, `addKeyPath` is a key path that references the add method of `Calculator` as a metatype member. The key path’s root type is `Calculator.Type`, and the value resolves to an applied instance method result type of`Int`.
105
97
106
98
### Overloads
107
99
108
-
Keypaths to methods with the same base name and distinct argument labels can be disambiguated by explicitly including the argument labels:
100
+
Keypaths to methods with the same base name and distinct argument labels can be disambiguated with explicit argument labels:
109
101
110
102
```swift
111
103
structCalculator {
@@ -114,10 +106,25 @@ struct Calculator {
114
106
funcsubtract(that: Int) ->Int { that + that }
115
107
}
116
108
117
-
let kp1 = \S.subtract// KeyPath<S, (Int, Int) -> Int
118
-
let kp2 = \S.subtract(this:) // WritableKeyPath<S, (Int) -> Int>
119
-
let kp3 = \S.subtract(that:) // WritableKeyPath<S, (Int) -> Int>
120
-
let kp4 = \S.subtract(that: 1) // WritableKeyPath<S, Int>
109
+
let kp1 = \Calculator.subtract// KeyPath<Calculator, (Int, Int) -> Int
110
+
let kp2 = \Calculator.subtract(this:) // KeyPath<Calculator, (Int) -> Int>
111
+
let kp3 = \Calculator.subtract(that:) // KeyPath<Calculator, (Int) -> Int>
112
+
let kp4 = \Calculator.subtract(that: 1) // KeyPath<Calculator, Int>
113
+
```
114
+
115
+
### Implicit closure conversion
116
+
117
+
This feature also supports implicit closure conversion of key path methods, allowing them to used in expressions where closures are expected, such as in higher order functions:
118
+
119
+
```swift
120
+
structCalculator {
121
+
funcpower(ofbase: Int, exponent: Int) ->Int {
122
+
returnInt(pow(Double(base), Double(exponent)))
123
+
}
124
+
}
125
+
126
+
let calculators = [Calculator(), Calculator()]
127
+
let results = calculators.map(\.power(of: 2, exponent: 3))
121
128
```
122
129
123
130
### Dynamic member lookups
@@ -141,7 +148,7 @@ print(subtract(10))
141
148
142
149
### Effectful value types
143
150
144
-
Methods annotated with `nonisolated` and `consuming` are supported by this feature. `mutating`, `throwing` and `async` are not supported for any other component type and will similarly not be supported for methods. Keypaths cannot capture method arguments that are not `Hashable`/`Equatable`, so `escaping` is also not supported.
151
+
Methods annotated with `nonisolated` and `consuming` are supported by this feature. However, noncopying root and value types [are not supported](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0437-noncopyable-stdlib-primitives.md#additional-future-work). `mutating`, `throws` and `async` are not supported for any other component type and will similarly not be supported for methods. Additionally keypaths cannot capture closure arguments that are not `Hashable`/`Equatable`.
0 commit comments