-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathkeypath.swift
405 lines (352 loc) · 10.8 KB
/
keypath.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
class MyLabel: Hashable {
var text = "label"
static var isVisible = true
func x(val value: Int) -> Int { return value }
static func y(val value: Int) -> Int { return value }
func saveClosure(_ closure: @escaping () -> Void) {
storedClosure = closure
}
func executeStoredClosure() {
storedClosure?()
}
private var storedClosure: (() -> Void)?
required init() {}
required init(customText: String) {
text = customText
}
static func == (lhs: MyLabel, rhs: MyLabel) -> Bool {
return lhs === rhs
}
func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
}
class Controller {
fileprivate let label = MyLabel()
fileprivate var secondLabel: MyLabel? = MyLabel()
public var thirdLabel: MyLabel? = MyLabel()
fileprivate var fourthLabel: MyLabel.Type? { return MyLabel.self }
public var fifthLabel: MyLabel.Type? { return MyLabel.self }
subscript(string: String) -> String {
get {
""
}
set {
}
}
subscript(int int: Int, str str: String, otherInt: Int) -> Int {
get {
0
}
set {
}
}
subscript() -> Int {
0
}
subscript<T>(array: [T]) -> T? {
array.first
}
subscript<T, U>(array: [T], array2: [U]) -> T? {
array.first
}
subscript<T>(array array: [T]) -> T? {
array.first
}
subscript<T, U>(array array: [T], array2 array2: [U]) -> T? {
array.first
}
}
struct Container<V> {
var v : V
init(_ v: V) {
self.v = v
}
func useKeyPath<V2: AnyObject>(_ keyPath: KeyPath<V, V2>) -> String {
return (v[keyPath: keyPath] as! MyLabel).text
}
func invokeKeyPathMethod<V2, R>(
_ keyPath: KeyPath<V, V2>,
method: KeyPath<V2, (Int) -> R>,
arg: Int
) -> R {
let instance = v[keyPath: keyPath]
return instance[keyPath: method](arg)
}
}
extension Container where V: Controller {
func test() -> String {
return useKeyPath(\.label)
}
func testKeyPathMethod() -> Int {
let result = invokeKeyPathMethod(\.label, method: \MyLabel.x(val:), arg: 10)
return result
}
}
// CHECK: label
print(Container(Controller()).test())
// CHECK: 10
print(Container(Controller()).testKeyPathMethod())
struct MetatypeContainer<V> {
var v : V.Type
init(_ v: V.Type) {
self.v = v
}
func useMetatypeKeyPath() -> Bool {
if let labelType = v as? MyLabel.Type {
return labelType.isVisible
}
return false
}
func getKeyPathMethodVal() -> Int {
if let labelType = v as? MyLabel.Type {
return labelType.y(val: 20)
}
return 0
}
func createInstanceWithDefaultInit() -> MyLabel? {
if let labelType = v as? MyLabel.Type {
return labelType.init()
}
return nil
}
func createInstanceWithCustomInit(customText: String) -> MyLabel? {
if let labelType = v as? MyLabel.Type {
return labelType.init(customText: customText)
}
return nil
}
}
// CHECK: true
print(MetatypeContainer(MyLabel.self).useMetatypeKeyPath())
// CHECK: 20
print(MetatypeContainer(MyLabel.self).getKeyPathMethodVal())
// CHECK: label
if let instance = MetatypeContainer(MyLabel.self).createInstanceWithDefaultInit() {
print(instance.text)
}
// CHECK: Custom Label
if let customInstance = MetatypeContainer(MyLabel.self).createInstanceWithCustomInit(customText: "Custom Label") {
print(customInstance.text)
}
public class GenericController<U> {
init(_ u: U) {
self.u = u
}
var u : U
fileprivate let label = MyLabel()
}
public func generic_class_constrained_keypath<U, V>(_ c: V) where V : GenericController<U> {
let kp = \V.label
print(kp)
print(c[keyPath: kp].text)
}
// CHECK: GenericController<Int>.label
// CHECK: label
generic_class_constrained_keypath(GenericController(5))
struct S {
var year = 2024
static let millenium: Int = 3
init() {}
init(val value: Int = 2024) { year = value }
var add: (Int, Int) -> Int { return { $0 + $1 } }
func add(this: Int) -> Int { this + this}
func add(that: Int) -> Int { that + that }
static func subtract(_ val: Int) -> Int { return millenium - val }
nonisolated func nonisolatedNextYear() -> Int { year + 1 }
consuming func consume() { print(year) }
subscript(index: Int) -> Int { return year + index}
}
// CHECK: {{\\Controller\.secondLabel!\.text|\\Controller\.<computed 0x.* \(Optional<MyLabel>\)>!\.<computed 0x.* \(String\)>}}
print(\Controller.secondLabel!.text)
// CHECK: {{\\Controller\.subscript\(_: String\)|\\Controller\.<computed 0x.* \(String\)>}}
print(\Controller["abc"])
// CHECK: \S.year
print(\S.year)
// CHECK: {{\\Controller\.subscript\(int: Int, str: String, _: Int\)|\\Controller\.<computed 0x.* \(Int\)>}}
print(\Controller[int: 0, str: "", 0])
// CHECK: {{\\Controller\.thirdLabel|\\Controller\.<computed 0x.* \(Optional<MyLabel>\)>}}
print(\Controller.thirdLabel)
// CHECK: {{\\Controller\.subscript\(\)|\\Controller\.<computed 0x.* \(Int\)>}}
print(\Controller.[])
// CHECK: \Controller.self
print(\Controller.self)
// Subscripts with dependent generic types don't produce good output currently,
// so we're just checking to make sure they don't crash.
// CHECK: \Controller.
print(\Controller[[42]])
// CHECK: Controller.
print(\Controller[[42], [42]])
// CHECK: \Controller.
print(\Controller[array: [42]])
// CHECK: \Controller.
print(\Controller[array: [42], array2: [42]])
// CHECK: {{\\Controller\.(fourthLabel|<computed .* \(Optional<MyLabel\.Type>\)>)!\.<computed .* \(Bool\)>}}
print(\Controller.fourthLabel!.isVisible)
// CHECK: \S.Type.<computed {{.*}} (Int)>
print(\S.Type.millenium)
// CHECK: {{\\Controller\.(fifthLabel|<computed .* \(Optional<MyLabel\.Type>\)>)\?\.<computed .* \(Bool\)>?}}
print(\Controller.fifthLabel?.isVisible)
// CHECK: \S.Type.<computed {{.*}} (() -> S)>
print(\S.Type.init)
// CHECK: \S.Type.<computed {{.*}} (S)>
print(\S.Type.init())
// CHECK: \S.Type.<computed {{.*}} ((Int) -> S)>
print(\S.Type.init(val:))
// CHECK: \S.Type.<computed {{.*}} (S)>
print(\S.Type.init(val: 2025))
// CHECK: \S.Type.<computed {{.*}} (S)>.year
print(\S.Type.init(val: 2025).year)
// CHECK: \S.Type.<computed {{.*}} (S)>.subscript(_: Int)
print(\S.Type.init()[0])
// CHECK: \S.add
print(\S.add)
// CHECK: \S.<computed {{.*}} ((Int) -> Int)>
print(\S.add(this:))
// CHECK: \S.<computed {{.*}} (Int)>
print(\S.add(that: 1))
// CHECK: \S.Type.<computed {{.*}} ((Int) -> Int)>
print(\S.Type.subtract)
// CHECK: \S.Type.<computed {{.*}} (Int)>
print(\S.Type.subtract(1))
// CHECK: \S.<computed {{.*}} (() -> Int)>
print(\S.nonisolatedNextYear)
// CHECK: \S.<computed {{.*}} (Int)>
print(\S.nonisolatedNextYear())
// CHECK: \S.Type.<computed {{.*}} (S)>.<computed {{.*}} (Int)>
print(\S.Type.init(val:2025).nonisolatedNextYear())
// CHECK: \S.Type.<computed {{.*}} (S)>.<computed {{.*}} (Int)>.description
print(\S.Type.init(val:2025).nonisolatedNextYear().description)
// CHECK: \S.Type.<computed {{.*}} (S)>.<computed {{.*}} (Int)>.<computed {{.*}} (Int)>
print(\S.Type.init(val:2025).nonisolatedNextYear().signum())
// // CHECK: \S.<computed {{.*}} (())>
print(\S.consume())
// CHECK: false
print(\S.add(that: 1) == \S.add(this: 1))
// CHECK: false
print(\S.add(that: 1) == \S.add(this: 2))
// CHECK: false
print(\S.Type.init(val: 2024) == \S.Type.init(val: 2025))
// CHECK: false
print(\S.Type.init(val: 2024).nonisolatedNextYear() == \S.Type.init(val: 2025))
// CHECK: true
print(\S.Type.init(val: 2024).add(this: 1) != \S.Type.init(val: 2025))
class E: Hashable {
static func == (lhs: E, rhs: E) -> Bool { return lhs === rhs }
func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
}
struct BaseType {
func foo(hashableParam e: E) {}
}
let hashableInstance = E()
// CHECK: \BaseType.<computed {{.*}} (())>
print(\BaseType.foo(hashableParam: hashableInstance))
protocol Describable {
func describe() -> String
}
struct C: Describable {
var name: String
func describe() -> String { return "\(name)" }
}
// CHECK: \C.<computed {{.*}} (() -> String)>
print(\C.describe)
// CHECK: false
print(\S.Type.init(val:2025) == \S.Type.init(val:2026))
// CHECK: false
print(\S.Type.init(val:2025).nonisolatedNextYear() == \S.Type.init(val:2026).nonisolatedNextYear())
// CHECK: true
print(\S.Type.init(val:2025).nonisolatedNextYear() == \S.Type.init(val:2025).nonisolatedNextYear())
// CHECK: false
print(\MyLabel.x(val:10) == \MyLabel.x(val:20))
// CHECK: true
print(\MyLabel.Type.y(val:10) == \MyLabel.Type.y(val:10))
do {
struct S {
var i: Int
}
func test<T, U>(v: T, _ kp: any KeyPath<T, U> & Sendable) {
print(v[keyPath: kp])
}
// CHECK: 42
test(v: S(i: 42), \.i)
}
do {
@dynamicMemberLookup
struct Test<T> {
var obj: T
subscript<U>(dynamicMember member: KeyPath<T, U> & Sendable) -> U {
get { obj[keyPath: member] }
}
}
// CHECK: 5
print(Test(obj: "Hello").utf8.count)
}
do {
struct S1: Hashable {}
struct S2 {
subscript(param: S1) -> String { "Subscript with private type" }
}
let kp = \S2[S1()]
// CHECK: Subscript with private type
print(S2()[keyPath: kp])
// CHECK: {{\\S2\.subscript\(_: S1 #[0-9]+\)|\S2\.<computed 0x.* \(String\)>}}
print(kp)
}
do {
struct Weekday {
static let day = "Monday"
}
@dynamicMemberLookup
struct StaticExample<T> {
subscript<U>(dynamicMember keyPath: KeyPath<T.Type, U>) -> U {
return T.self[keyPath: keyPath]
}
}
// CHECK: true
print(StaticExample<MyLabel>().isVisible)
}
do {
@dynamicMemberLookup
struct InstanceDynamicMemberLookup<T> {
var obj: T
subscript<U>(dynamicMember member: KeyPath<T, (Int) -> U>) -> (Int) -> U {
get { obj[keyPath: member] }
}
}
// CHECK: 50
let instanceDynamicLookup = InstanceDynamicMemberLookup(obj: MyLabel())
print(instanceDynamicLookup.x(50))
}
extension MyLabel {
static var defaultInitializer: () -> MyLabel { return MyLabel.init }
static var customInitializer: (String) -> MyLabel { return MyLabel.init(customText:) }
}
do {
@dynamicMemberLookup
struct StaticDynamicMemberLookup<T> {
subscript<U>(dynamicMember keyPath: KeyPath<T.Type, (Int) -> U>) -> (Int) -> U {
return T.self[keyPath: keyPath]
}
subscript<U>(dynamicMember keyPath: KeyPath<T.Type, () -> U>) -> () -> U {
return T.self[keyPath: keyPath]
}
subscript<U>(dynamicMember keyPath: KeyPath<T.Type, (String) -> U>) -> (String) -> U {
return T.self[keyPath: keyPath]
}
}
// CHECK: 60
let staticDynamicLookup = StaticDynamicMemberLookup<MyLabel>()
print(staticDynamicLookup.y(60))
// CHECK: label
let defaultInstance = staticDynamicLookup.defaultInitializer()
print(defaultInstance.text)
// CHECK: Custom Label
let customInstance = staticDynamicLookup.customInitializer("Custom Label")
print(customInstance.text)
}