Skip to content

Commit b26108c

Browse files
Merge pull request #42361 from AnthonyLatsis/5.7-changelog-dynamic-lookup-unbound-ref
🍒 [5.7] Add CHANGELOG entry for #41849 and #42332
2 parents 31967c6 + 5deab08 commit b26108c

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

CHANGELOG.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
55

66
## Swift 5.7
77

8+
* References to `optional` methods on a protocol metatype, as well as references to dynamically looked up methods on the `AnyObject` metatype are now supported. These references always have the type of a function that accepts a single argument and returns an optional value of function type:
9+
10+
```swift
11+
class Object {
12+
@objc func getTag() -> Int
13+
}
14+
15+
@objc protocol P {
16+
@objc optional func didUpdateObject(withTag tag: Int)
17+
}
18+
19+
let getTag: (AnyObject) -> (() -> Int)? = AnyObject.getTag
20+
21+
let didUpdateObject: (any P) -> ((Int) -> Void)? = P.didUpdateObject
22+
```
23+
824
* [SE-0352][]:
925

1026
It's now possible to call a generic function with a value of protocol type
@@ -32,7 +48,7 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
3248
It's now possible to use a default value expression with a generic parameter type
3349
to default the argument and its type:
3450

35-
```
51+
```swift
3652
func compute<C: Collection>(_ values: C = [0, 1, 2]) {
3753
...
3854
}

test/Interpreter/dynamic_lookup.swift

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
import Foundation
77

8+
@objc protocol P {
9+
@objc optional func e()
10+
}
11+
812
class X {
913
init() {}
1014

@@ -15,11 +19,15 @@ class X {
1519
return 17
1620
}
1721
}
22+
extension X: P {
23+
@objc func e() { print("X.e()") }
24+
}
1825

1926
class Y {
2027
init() {}
2128
@objc class func g() { print("Y.g()") }
2229
}
30+
extension Y: P {}
2331

2432
class Z {
2533
init() {}
@@ -44,7 +52,16 @@ func test_dynamic_lookup_f_unbound(_ obj: AnyObject) {
4452
if of != nil {
4553
of!()
4654
} else {
47-
print("Object does not respond to the selector \"f\".\n", terminator: "")
55+
print("\(type(of: obj)) does not respond to the selector \"f\"")
56+
}
57+
}
58+
59+
func test_dynamic_lookup_e_unbound(_ obj: AnyObject) {
60+
var oe = AnyObject.e(obj)
61+
if oe != nil {
62+
oe!()
63+
} else {
64+
print("\(type(of: obj)) does not respond to the selector \"e\"")
4865
}
4966
}
5067

@@ -77,11 +94,20 @@ test_dynamic_lookup_f(Z())
7794
print(type(of: AnyObject.f))
7895
// CHECK-NEXT: X.f()
7996
test_dynamic_lookup_f_unbound(X())
80-
// CHECK-NEXT: Object does not respond to the selector "f"
97+
// CHECK-NEXT: Y does not respond to the selector "f"
8198
test_dynamic_lookup_f_unbound(Y())
8299
// CHECK-NEXT: Z.f()
83100
test_dynamic_lookup_f_unbound(Z())
84101

102+
// CHECK-NEXT: (AnyObject) -> Optional<() -> ()>
103+
print(type(of: AnyObject.e))
104+
// CHECK-NEXT: X.e()
105+
test_dynamic_lookup_e_unbound(X())
106+
// CHECK-NEXT: Y does not respond to the selector "e"
107+
test_dynamic_lookup_e_unbound(Y())
108+
// CHECK-NEXT: Z does not respond to the selector "e"
109+
test_dynamic_lookup_e_unbound(Z())
110+
85111
// CHECK: Class does not respond to the selector "g"
86112
test_dynamic_lookup_g(X())
87113
// CHECK: Y.g()

0 commit comments

Comments
 (0)