Skip to content

Commit cd04e26

Browse files
authored
Merge pull request swiftlang#9205 from huonw/keypath-syntax
2 parents fa4e4be + a6f5f16 commit cd04e26

File tree

2 files changed

+52
-52
lines changed

2 files changed

+52
-52
lines changed

test/stdlib/KeyPath.swift

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,24 @@ struct ComputedB {
7575

7676
keyPath.test("key path in-place instantiation") {
7777
for _ in 1...2 {
78-
let s_x = (#keyPath2(S<Int>, .x) as AnyKeyPath) as! WritableKeyPath<S<Int>, Int>
79-
let s_y = (#keyPath2(S<Int>, .y) as AnyKeyPath) as! WritableKeyPath<S<Int>, LifetimeTracked?>
80-
let s_z = (#keyPath2(S<Int>, .z) as AnyKeyPath) as! WritableKeyPath<S<Int>, Int>
81-
let s_p = (#keyPath2(S<Int>, .p) as AnyKeyPath) as! WritableKeyPath<S<Int>, Point>
82-
let s_p_x = (#keyPath2(S<Int>, .p.x) as AnyKeyPath) as! WritableKeyPath<S<Int>, Double>
83-
let s_p_y = (#keyPath2(S<Int>, .p.y) as AnyKeyPath) as! WritableKeyPath<S<Int>, Double>
84-
let s_c = (#keyPath2(S<Int>, .c) as AnyKeyPath) as! WritableKeyPath<S<Int>, C<Int>>
85-
let s_c_x = (#keyPath2(S<Int>, .c.x) as AnyKeyPath) as! ReferenceWritableKeyPath<S<Int>, Int>
86-
87-
let c_x = (#keyPath2(C<Int>, .x) as AnyKeyPath) as! ReferenceWritableKeyPath<C<Int>, Int>
78+
let s_x = (\S<Int>.x as AnyKeyPath) as! WritableKeyPath<S<Int>, Int>
79+
let s_y = (\S<Int>.y as AnyKeyPath) as! WritableKeyPath<S<Int>, LifetimeTracked?>
80+
let s_z = (\S<Int>.z as AnyKeyPath) as! WritableKeyPath<S<Int>, Int>
81+
let s_p = (\S<Int>.p as AnyKeyPath) as! WritableKeyPath<S<Int>, Point>
82+
let s_p_x = (\S<Int>.p.x as AnyKeyPath) as! WritableKeyPath<S<Int>, Double>
83+
let s_p_y = (\S<Int>.p.y as AnyKeyPath) as! WritableKeyPath<S<Int>, Double>
84+
let s_c = (\S<Int>.c as AnyKeyPath) as! WritableKeyPath<S<Int>, C<Int>>
85+
let s_c_x = (\S<Int>.c.x as AnyKeyPath) as! ReferenceWritableKeyPath<S<Int>, Int>
86+
87+
let c_x = (\C<Int>.x as AnyKeyPath) as! ReferenceWritableKeyPath<C<Int>, Int>
8888
let s_c_x_2 = s_c.appending(path: c_x)
8989

9090
expectEqual(s_c_x, s_c_x_2)
9191
expectEqual(s_c_x_2, s_c_x)
9292
expectEqual(s_c_x.hashValue, s_c_x_2.hashValue)
9393

94-
let point_x = (#keyPath2(Point, .x) as AnyKeyPath) as! WritableKeyPath<Point, Double>
95-
let point_y = (#keyPath2(Point, .y) as AnyKeyPath) as! WritableKeyPath<Point, Double>
94+
let point_x = (\Point.x as AnyKeyPath) as! WritableKeyPath<Point, Double>
95+
let point_y = (\Point.y as AnyKeyPath) as! WritableKeyPath<Point, Double>
9696

9797
let s_p_x_2 = s_p.appending(path: point_x)
9898
let s_p_y_2 = s_p.appending(path: point_y)
@@ -104,19 +104,19 @@ keyPath.test("key path in-place instantiation") {
104104
expectEqual(s_p_y_2, s_p_y)
105105
expectEqual(s_p_y_2.hashValue, s_p_y.hashValue)
106106

107-
let ca_readOnly = (#keyPath2(ComputedA, .readOnly) as AnyKeyPath) as! KeyPath<ComputedA, ComputedB>
108-
let ca_nonmutating = (#keyPath2(ComputedA, .nonmutating) as AnyKeyPath) as! ReferenceWritableKeyPath<ComputedA, ComputedB>
109-
let ca_reabstracted = (#keyPath2(ComputedA, .reabstracted) as AnyKeyPath) as! ReferenceWritableKeyPath<ComputedA, () -> ()>
107+
let ca_readOnly = (\ComputedA.readOnly as AnyKeyPath) as! KeyPath<ComputedA, ComputedB>
108+
let ca_nonmutating = (\ComputedA.nonmutating as AnyKeyPath) as! ReferenceWritableKeyPath<ComputedA, ComputedB>
109+
let ca_reabstracted = (\ComputedA.reabstracted as AnyKeyPath) as! ReferenceWritableKeyPath<ComputedA, () -> ()>
110110

111-
let cb_readOnly = (#keyPath2(ComputedB, .readOnly) as AnyKeyPath) as! KeyPath<ComputedB, ComputedA>
112-
let cb_mutating = (#keyPath2(ComputedB, .mutating) as AnyKeyPath) as! WritableKeyPath<ComputedB, ComputedA>
113-
let cb_nonmutating = (#keyPath2(ComputedB, .nonmutating) as AnyKeyPath) as! ReferenceWritableKeyPath<ComputedB, ComputedA>
114-
let cb_reabstracted = (#keyPath2(ComputedB, .reabstracted) as AnyKeyPath) as! WritableKeyPath<ComputedB, () -> ()>
111+
let cb_readOnly = (\ComputedB.readOnly as AnyKeyPath) as! KeyPath<ComputedB, ComputedA>
112+
let cb_mutating = (\ComputedB.mutating as AnyKeyPath) as! WritableKeyPath<ComputedB, ComputedA>
113+
let cb_nonmutating = (\ComputedB.nonmutating as AnyKeyPath) as! ReferenceWritableKeyPath<ComputedB, ComputedA>
114+
let cb_reabstracted = (\ComputedB.reabstracted as AnyKeyPath) as! WritableKeyPath<ComputedB, () -> ()>
115115

116-
let ca_readOnly_mutating = (#keyPath2(ComputedA, .readOnly.mutating) as AnyKeyPath) as! KeyPath<ComputedA, ComputedA>
117-
let cb_mutating_readOnly = (#keyPath2(ComputedB, .mutating.readOnly) as AnyKeyPath) as! KeyPath<ComputedB, ComputedB>
118-
let ca_readOnly_nonmutating = (#keyPath2(ComputedA, .readOnly.nonmutating) as AnyKeyPath) as! ReferenceWritableKeyPath<ComputedA, ComputedA>
119-
let cb_readOnly_reabstracted = (#keyPath2(ComputedB, .readOnly.reabstracted) as AnyKeyPath) as! ReferenceWritableKeyPath<ComputedB, () -> ()>
116+
let ca_readOnly_mutating = (\ComputedA.readOnly.mutating as AnyKeyPath) as! KeyPath<ComputedA, ComputedA>
117+
let cb_mutating_readOnly = (\ComputedB.mutating.readOnly as AnyKeyPath) as! KeyPath<ComputedB, ComputedB>
118+
let ca_readOnly_nonmutating = (\ComputedA.readOnly.nonmutating as AnyKeyPath) as! ReferenceWritableKeyPath<ComputedA, ComputedA>
119+
let cb_readOnly_reabstracted = (\ComputedB.readOnly.reabstracted as AnyKeyPath) as! ReferenceWritableKeyPath<ComputedB, () -> ()>
120120

121121
let ca_readOnly_mutating2 = ca_readOnly.appending(path: cb_mutating)
122122
expectEqual(ca_readOnly_mutating, ca_readOnly_mutating2)
@@ -147,24 +147,24 @@ keyPath.test("key path in-place instantiation") {
147147
keyPath.test("key path generic instantiation") {
148148
func testWithGenericParam<T: Equatable>(_: T.Type) -> ReferenceWritableKeyPath<S<T>, Int> {
149149
for i in 1...2 {
150-
let s_x = (#keyPath2(S<T>, .x) as AnyKeyPath) as! WritableKeyPath<S<T>, Int>
151-
let s_y = (#keyPath2(S<T>, .y) as AnyKeyPath) as! WritableKeyPath<S<T>, LifetimeTracked?>
152-
let s_z = (#keyPath2(S<T>, .z) as AnyKeyPath) as! WritableKeyPath<S<T>, T>
153-
let s_p = (#keyPath2(S<T>, .p) as AnyKeyPath) as! WritableKeyPath<S<T>, Point>
154-
let s_p_x = (#keyPath2(S<T>, .p.x) as AnyKeyPath) as! WritableKeyPath<S<T>, Double>
155-
let s_p_y = (#keyPath2(S<T>, .p.y) as AnyKeyPath) as! WritableKeyPath<S<T>, Double>
156-
let s_c = (#keyPath2(S<T>, .c) as AnyKeyPath) as! WritableKeyPath<S<T>, C<T>>
157-
let s_c_x = (#keyPath2(S<T>, .c.x) as AnyKeyPath) as! ReferenceWritableKeyPath<S<T>, Int>
158-
159-
let c_x = (#keyPath2(C<T>, .x) as AnyKeyPath) as! ReferenceWritableKeyPath<C<T>, Int>
150+
let s_x = (\S<T>.x as AnyKeyPath) as! WritableKeyPath<S<T>, Int>
151+
let s_y = (\S<T>.y as AnyKeyPath) as! WritableKeyPath<S<T>, LifetimeTracked?>
152+
let s_z = (\S<T>.z as AnyKeyPath) as! WritableKeyPath<S<T>, T>
153+
let s_p = (\S<T>.p as AnyKeyPath) as! WritableKeyPath<S<T>, Point>
154+
let s_p_x = (\S<T>.p.x as AnyKeyPath) as! WritableKeyPath<S<T>, Double>
155+
let s_p_y = (\S<T>.p.y as AnyKeyPath) as! WritableKeyPath<S<T>, Double>
156+
let s_c = (\S<T>.c as AnyKeyPath) as! WritableKeyPath<S<T>, C<T>>
157+
let s_c_x = (\S<T>.c.x as AnyKeyPath) as! ReferenceWritableKeyPath<S<T>, Int>
158+
159+
let c_x = (\C<T>.x as AnyKeyPath) as! ReferenceWritableKeyPath<C<T>, Int>
160160
let s_c_x_2 = s_c.appending(path: c_x)
161161

162162
expectEqual(s_c_x, s_c_x_2)
163163
expectEqual(s_c_x_2, s_c_x)
164164
expectEqual(s_c_x.hashValue, s_c_x_2.hashValue)
165165

166-
let point_x = (#keyPath2(Point, .x) as AnyKeyPath) as! WritableKeyPath<Point, Double>
167-
let point_y = (#keyPath2(Point, .y) as AnyKeyPath) as! WritableKeyPath<Point, Double>
166+
let point_x = (\Point.x as AnyKeyPath) as! WritableKeyPath<Point, Double>
167+
let point_y = (\Point.y as AnyKeyPath) as! WritableKeyPath<Point, Double>
168168

169169
let s_p_x_2 = s_p.appending(path: point_x)
170170
let s_p_y_2 = s_p.appending(path: point_y)
@@ -181,15 +181,15 @@ keyPath.test("key path generic instantiation") {
181181
fatalError()
182182
}
183183
let s_c_x_int = testWithGenericParam(Int.self)
184-
let s_c_x_int2 = #keyPath2(S<Int>, .c.x)
184+
let s_c_x_int2 = \S<Int>.c.x
185185
expectEqual(s_c_x_int, s_c_x_int2)
186186

187187
let s_c_x_string = testWithGenericParam(String.self)
188-
let s_c_x_string2 = #keyPath2(S<String>, .c.x)
188+
let s_c_x_string2 = \S<String>.c.x
189189
expectEqual(s_c_x_string, s_c_x_string2)
190190

191191
let s_c_x_lt = testWithGenericParam(LifetimeTracked.self)
192-
let s_c_x_lt2 = #keyPath2(S<LifetimeTracked>, .c.x)
192+
let s_c_x_lt2 = \S<LifetimeTracked>.c.x
193193
expectEqual(s_c_x_lt, s_c_x_lt2)
194194
}
195195

test/stdlib/KeyPathObjC.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@ class Bar: NSObject {
3030
var testKVCStrings = TestSuite("KVC strings")
3131

3232
testKVCStrings.test("KVC strings") {
33-
expectEqual((#keyPath2(NonObjC, .x))._kvcKeyPathString, nil)
34-
expectEqual((#keyPath2(NonObjC, .y))._kvcKeyPathString, nil)
35-
expectEqual((#keyPath2(Foo, .int))._kvcKeyPathString, "int")
36-
expectEqual((#keyPath2(Foo, .bar))._kvcKeyPathString, "bar")
37-
expectEqual((#keyPath2(Foo, .bar.foo))._kvcKeyPathString, "bar.foo")
38-
expectEqual((#keyPath2(Foo, .bar.foo.bar))._kvcKeyPathString, "bar.foo.bar")
39-
expectEqual((#keyPath2(Foo, .nonobjc))._kvcKeyPathString, nil)
40-
expectEqual((#keyPath2(Foo, .bar.foo.nonobjc.y))._kvcKeyPathString, nil)
41-
expectEqual((#keyPath2(Foo, .differentName))._kvcKeyPathString, "thisIsADifferentName")
42-
expectEqual((#keyPath2(Bar, .foo))._kvcKeyPathString, "foo")
33+
expectEqual((\NonObjC.x)._kvcKeyPathString, nil)
34+
expectEqual((\NonObjC.y)._kvcKeyPathString, nil)
35+
expectEqual((\Foo.int)._kvcKeyPathString, "int")
36+
expectEqual((\Foo.bar)._kvcKeyPathString, "bar")
37+
expectEqual((\Foo.bar.foo)._kvcKeyPathString, "bar.foo")
38+
expectEqual((\Foo.bar.foo.bar)._kvcKeyPathString, "bar.foo.bar")
39+
expectEqual((\Foo.nonobjc)._kvcKeyPathString, nil)
40+
expectEqual((\Foo.bar.foo.nonobjc.y)._kvcKeyPathString, nil)
41+
expectEqual((\Foo.differentName)._kvcKeyPathString, "thisIsADifferentName")
42+
expectEqual((\Bar.foo)._kvcKeyPathString, "foo")
4343

44-
let foo_bar = #keyPath2(Foo, .bar)
45-
let foo_nonobjc = #keyPath2(Foo, .nonobjc)
46-
let bar_foo = #keyPath2(Bar, .foo)
44+
let foo_bar = \Foo.bar
45+
let foo_nonobjc = \Foo.nonobjc
46+
let bar_foo = \Bar.foo
4747

48-
let nonobjc_y = #keyPath2(NonObjC, .y)
48+
let nonobjc_y = \NonObjC.y
4949

5050
do {
5151
let foo_bar_foo = foo_bar.appending(path: bar_foo)

0 commit comments

Comments
 (0)