5
5
// UNSUPPORTED: use_os_stdlib
6
6
// UNSUPPORTED: back_deployment_runtime
7
7
8
- class MyLabel {
8
+ class MyLabel : Hashable {
9
9
var text = " label "
10
10
static var isVisible = true
11
+ func x( val value: Int ) -> Int { return value }
12
+ static func y( val value: Int ) -> Int { return value }
13
+ func saveClosure( _ closure: @escaping ( ) -> Void ) {
14
+ storedClosure = closure
15
+ }
16
+ func executeStoredClosure( ) {
17
+ storedClosure ? ( )
18
+ }
19
+ private var storedClosure : ( ( ) -> Void ) ?
20
+
21
+ required init ( ) { }
22
+ required init ( customText: String ) {
23
+ text = customText
24
+ }
25
+
26
+ static func == ( lhs: MyLabel , rhs: MyLabel ) -> Bool {
27
+ return lhs === rhs
28
+ }
29
+ func hash( into hasher: inout Hasher ) {
30
+ hasher. combine ( ObjectIdentifier ( self ) )
31
+ }
11
32
}
12
33
13
34
class Controller {
@@ -57,11 +78,6 @@ class Controller {
57
78
}
58
79
}
59
80
60
- struct S {
61
- var a : Int
62
- static let b : Double = 100.0
63
- }
64
-
65
81
struct Container < V> {
66
82
var v : V
67
83
init ( _ v: V ) {
@@ -70,16 +86,30 @@ struct Container<V> {
70
86
func useKeyPath< V2: AnyObject > ( _ keyPath: KeyPath < V , V2 > ) -> String {
71
87
return ( v [ keyPath: keyPath] as! MyLabel ) . text
72
88
}
89
+ func invokeKeyPathMethod< V2, R> (
90
+ _ keyPath: KeyPath < V , V2 > ,
91
+ method: KeyPath < V2 , ( Int ) -> R > ,
92
+ arg: Int
93
+ ) -> R {
94
+ let instance = v [ keyPath: keyPath]
95
+ return instance [ keyPath: method] ( arg)
96
+ }
73
97
}
74
98
75
99
extension Container where V: Controller {
76
100
func test( ) -> String {
77
101
return useKeyPath ( \. label)
78
102
}
103
+ func testKeyPathMethod( ) -> Int {
104
+ let result = invokeKeyPathMethod ( \. label, method: \MyLabel . x ( val: ) , arg: 10 )
105
+ return result
106
+ }
79
107
}
80
108
81
109
// CHECK: label
82
110
print ( Container ( Controller ( ) ) . test ( ) )
111
+ // CHECK: 10
112
+ print ( Container ( Controller ( ) ) . testKeyPathMethod ( ) )
83
113
84
114
struct MetatypeContainer < V> {
85
115
var v : V . Type
@@ -92,10 +122,38 @@ struct MetatypeContainer<V> {
92
122
}
93
123
return false
94
124
}
125
+ func getKeyPathMethodVal( ) -> Int {
126
+ if let labelType = v as? MyLabel . Type {
127
+ return labelType. y ( val: 20 )
128
+ }
129
+ return 0
130
+ }
131
+ func createInstanceWithDefaultInit( ) -> MyLabel ? {
132
+ if let labelType = v as? MyLabel . Type {
133
+ return labelType. init ( )
134
+ }
135
+ return nil
136
+ }
137
+ func createInstanceWithCustomInit( customText: String ) -> MyLabel ? {
138
+ if let labelType = v as? MyLabel . Type {
139
+ return labelType. init ( customText: customText)
140
+ }
141
+ return nil
142
+ }
95
143
}
96
144
97
145
// CHECK: true
98
146
print ( MetatypeContainer ( MyLabel . self) . useMetatypeKeyPath ( ) )
147
+ // CHECK: 20
148
+ print ( MetatypeContainer ( MyLabel . self) . getKeyPathMethodVal ( ) )
149
+ // CHECK: label
150
+ if let instance = MetatypeContainer ( MyLabel . self) . createInstanceWithDefaultInit ( ) {
151
+ print ( instance. text)
152
+ }
153
+ // CHECK: Custom Label
154
+ if let customInstance = MetatypeContainer ( MyLabel . self) . createInstanceWithCustomInit ( customText: " Custom Label " ) {
155
+ print ( customInstance. text)
156
+ }
99
157
100
158
public class GenericController < U> {
101
159
init ( _ u: U ) {
@@ -116,13 +174,28 @@ public func generic_class_constrained_keypath<U, V>(_ c: V) where V : GenericCon
116
174
// CHECK: label
117
175
generic_class_constrained_keypath ( GenericController ( 5 ) )
118
176
177
+ struct S {
178
+ var year = 2024
179
+ static let millenium : Int = 3
180
+ init ( ) { }
181
+ init ( val value: Int = 2024 ) { year = value }
182
+
183
+ var add : ( Int , Int ) -> Int { return { $0 + $1 } }
184
+ func add( this: Int ) -> Int { this + this}
185
+ func add( that: Int ) -> Int { that + that }
186
+ static func subtract( _ val: Int ) -> Int { return millenium - val }
187
+ nonisolated func nonisolatedNextYear( ) -> Int { year + 1 }
188
+ consuming func consume( ) { print ( year) }
189
+ subscript( index: Int ) -> Int { return year + index}
190
+ }
191
+
119
192
// CHECK: {{\\Controller\.secondLabel!\.text|\\Controller\.<computed 0x.* \(Optional<MyLabel>\)>!\.<computed 0x.* \(String\)>}}
120
193
print ( \Controller . secondLabel!. text)
121
194
122
195
// CHECK: {{\\Controller\.subscript\(_: String\)|\\Controller\.<computed 0x.* \(String\)>}}
123
196
print ( \Controller [ " abc " ] )
124
- // CHECK: \S.a
125
- print ( \S . a )
197
+ // CHECK: \S.year
198
+ print ( \S . year )
126
199
// CHECK: {{\\Controller\.subscript\(int: Int, str: String, _: Int\)|\\Controller\.<computed 0x.* \(Int\)>}}
127
200
print ( \Controller [ int: 0 , str: " " , 0 ] )
128
201
// CHECK: {{\\Controller\.thirdLabel|\\Controller\.<computed 0x.* \(Optional<MyLabel>\)>}}
@@ -146,13 +219,92 @@ print(\Controller[array: [42], array2: [42]])
146
219
// CHECK: {{\\Controller\.(fourthLabel|<computed .* \(Optional<MyLabel\.Type>\)>)!\.<computed .* \(Bool\)>}}
147
220
print ( \Controller . fourthLabel!. isVisible)
148
221
149
- // CHECK: \S.Type.<computed {{.*}} (Double )>
150
- print ( \S . Type. b )
222
+ // CHECK: \S.Type.<computed {{.*}} (Int )>
223
+ print ( \S . Type. millenium )
151
224
// CHECK: {{\\Controller\.(fifthLabel|<computed .* \(Optional<MyLabel\.Type>\)>)\?\.<computed .* \(Bool\)>?}}
152
225
print ( \Controller . fifthLabel? . isVisible)
153
226
// CHECK: \Int.Type.<computed {{.*}} (Int)>
154
227
print ( \Int . Type. zero)
155
228
229
+ // CHECK: \S.Type.<computed {{.*}} (() -> S)>
230
+ print ( \S . Type. init)
231
+ // CHECK: \S.Type.<computed {{.*}} (S)>
232
+ print ( \S . Type. init ( ) )
233
+ // CHECK: \S.Type.<computed {{.*}} ((Int) -> S)>
234
+ print ( \S . Type. init ( val: ) )
235
+ // CHECK: \S.Type.<computed {{.*}} (S)>
236
+ print ( \S . Type. init ( val: 2025 ) )
237
+ // CHECK: \S.Type.<computed {{.*}} (S)>.year
238
+ print ( \S . Type. init ( val: 2025 ) . year)
239
+ // CHECK: \S.Type.<computed {{.*}} (S)>.subscript(_: Int)
240
+ print ( \S . Type. init ( ) [ 0 ] )
241
+ // CHECK: \S.add
242
+ print ( \S . add)
243
+ // CHECK: \S.<computed {{.*}} ((Int) -> Int)>
244
+ print ( \S . add ( this: ) )
245
+ // CHECK: \S.<computed {{.*}} (Int)>
246
+ print ( \S . add ( that: 1 ) )
247
+ // CHECK: \S.Type.<computed {{.*}} ((Int) -> Int)>
248
+ print ( \S . Type. subtract)
249
+ // CHECK: \S.Type.<computed {{.*}} (Int)>
250
+ print ( \S . Type. subtract ( 1 ) )
251
+ // CHECK: \S.<computed {{.*}} (() -> Int)>
252
+ print ( \S . nonisolatedNextYear)
253
+ // CHECK: \S.<computed {{.*}} (Int)>
254
+ print ( \S . nonisolatedNextYear ( ) )
255
+ // CHECK: \S.Type.<computed {{.*}} (S)>.<computed {{.*}} (Int)>
256
+ print ( \S . Type. init ( val: 2025 ) . nonisolatedNextYear ( ) )
257
+ // CHECK: \S.Type.<computed {{.*}} (S)>.<computed {{.*}} (Int)>.description
258
+ print ( \S . Type. init ( val: 2025 ) . nonisolatedNextYear ( ) . description)
259
+ // CHECK: \S.Type.<computed {{.*}} (S)>.<computed {{.*}} (Int)>.<computed {{.*}} (Int)>
260
+ print ( \S . Type. init ( val: 2025 ) . nonisolatedNextYear ( ) . signum ( ) )
261
+ // // CHECK: \S.<computed {{.*}} (())>
262
+ print ( \S . consume ( ) )
263
+
264
+ // CHECK: false
265
+ print ( \S . add ( that: 1 ) == \S . add ( this: 1 ) )
266
+ // CHECK: false
267
+ print ( \S . add ( that: 1 ) == \S . add ( this: 2 ) )
268
+ // CHECK: false
269
+ print ( \S . Type. init ( val: 2024 ) == \S . Type. init ( val: 2025 ) )
270
+ // CHECK: false
271
+ print ( \S . Type. init ( val: 2024 ) . nonisolatedNextYear ( ) == \S . Type. init ( val: 2025 ) )
272
+ // CHECK: true
273
+ print ( \S . Type. init ( val: 2024 ) . add ( this: 1 ) != \S . Type. init ( val: 2025 ) )
274
+
275
+ class E : Hashable {
276
+ static func == ( lhs: E , rhs: E ) -> Bool { return lhs === rhs }
277
+ func hash( into hasher: inout Hasher ) {
278
+ hasher. combine ( ObjectIdentifier ( self ) )
279
+ }
280
+ }
281
+ struct BaseType {
282
+ func foo( hashableParam e: E ) { }
283
+ }
284
+ let hashableInstance = E ( )
285
+ // CHECK: \BaseType.<computed {{.*}} (())>
286
+ print ( \BaseType . foo ( hashableParam: hashableInstance) )
287
+
288
+ protocol Describable {
289
+ func describe( ) -> String
290
+ }
291
+ struct C : Describable {
292
+ var name : String
293
+ func describe( ) -> String { return " \( name) " }
294
+ }
295
+ // CHECK: \C.<computed {{.*}} (() -> String)>
296
+ print ( \C . describe)
297
+
298
+ // CHECK: false
299
+ print ( \S . Type. init ( val: 2025 ) == \S . Type. init ( val: 2026 ) )
300
+ // CHECK: false
301
+ print ( \S . Type. init ( val: 2025 ) . nonisolatedNextYear ( ) == \S . Type. init ( val: 2026 ) . nonisolatedNextYear ( ) )
302
+ // CHECK: true
303
+ print ( \S . Type. init ( val: 2025 ) . nonisolatedNextYear ( ) == \S . Type. init ( val: 2025 ) . nonisolatedNextYear ( ) )
304
+ // CHECK: false
305
+ print ( \MyLabel . x ( val: 10 ) == \MyLabel . x ( val: 20 ) )
306
+ // CHECK: true
307
+ print ( \MyLabel . Type. y ( val: 10 ) == \MyLabel . Type. y ( val: 10 ) )
156
308
157
309
do {
158
310
struct S {
@@ -208,3 +360,48 @@ do {
208
360
// CHECK: true
209
361
print ( StaticExample < MyLabel > ( ) . isVisible)
210
362
}
363
+
364
+ do {
365
+ @dynamicMemberLookup
366
+ struct InstanceDynamicMemberLookup < T> {
367
+ var obj : T
368
+
369
+ subscript< U> ( dynamicMember member: KeyPath < T , ( Int ) -> U > ) -> ( Int ) -> U {
370
+ get { obj [ keyPath: member] }
371
+ }
372
+ }
373
+
374
+ // CHECK: 50
375
+ let instanceDynamicLookup = InstanceDynamicMemberLookup ( obj: MyLabel ( ) )
376
+ print ( instanceDynamicLookup. x ( 50 ) )
377
+ }
378
+
379
+ extension MyLabel {
380
+ static var defaultInitializer : ( ) -> MyLabel { return MyLabel . init }
381
+ static var customInitializer : ( String ) -> MyLabel { return MyLabel . init ( customText: ) }
382
+ }
383
+
384
+ do {
385
+ @dynamicMemberLookup
386
+ struct StaticDynamicMemberLookup < T> {
387
+ subscript< U> ( dynamicMember keyPath: KeyPath < T . Type , ( Int ) -> U > ) -> ( Int ) -> U {
388
+ return T . self [ keyPath: keyPath]
389
+ }
390
+ subscript< U> ( dynamicMember keyPath: KeyPath < T . Type , ( ) -> U > ) -> ( ) -> U {
391
+ return T . self [ keyPath: keyPath]
392
+ }
393
+ subscript< U> ( dynamicMember keyPath: KeyPath < T . Type , ( String ) -> U > ) -> ( String ) -> U {
394
+ return T . self [ keyPath: keyPath]
395
+ }
396
+ }
397
+
398
+ // CHECK: 60
399
+ let staticDynamicLookup = StaticDynamicMemberLookup < MyLabel > ( )
400
+ print ( staticDynamicLookup. y ( 60 ) )
401
+ // CHECK: label
402
+ let defaultInstance = staticDynamicLookup. defaultInitializer ( )
403
+ print ( defaultInstance. text)
404
+ // CHECK: Custom Label
405
+ let customInstance = staticDynamicLookup. customInitializer ( " Custom Label " )
406
+ print ( customInstance. text)
407
+ }
0 commit comments