forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_lookup.swift
134 lines (109 loc) · 2.82 KB
/
dynamic_lookup.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
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: objc_interop
import Foundation
@objc protocol P {
@objc optional func e()
}
class X {
init() {}
@objc func f() { print("X.f()") }
@objc var myValue : Int {
print("X.myValue getter\n")
return 17
}
}
extension X: P {
@objc func e() { print("X.e()") }
}
class Y {
init() {}
@objc class func g() { print("Y.g()") }
}
extension Y: P {}
class Z {
init() {}
}
extension Z {
@objc func f() { print("Z.f()") }
}
func test_dynamic_lookup_f(_ obj: AnyObject) {
var of = obj.f
if of != nil {
of!()
} else {
print("Object does not respond to the selector \"f\".\n", terminator: "")
}
}
func test_dynamic_lookup_f_unbound(_ obj: AnyObject) {
var of = AnyObject.f(obj)
if of != nil {
of!()
} else {
print("\(type(of: obj)) does not respond to the selector \"f\"")
}
}
func test_dynamic_lookup_e_unbound(_ obj: AnyObject) {
var oe = AnyObject.e(obj)
if oe != nil {
oe!()
} else {
print("\(type(of: obj)) does not respond to the selector \"e\"")
}
}
func test_dynamic_lookup_g(_ obj: AnyObject) {
var og = type(of: obj).g
if og != nil {
og!()
} else {
print("Class does not respond to the selector \"g\".\n", terminator: "")
}
}
func test_dynamic_lookup_myValue(_ obj: AnyObject) {
var ov = obj.myValue
if ov != nil {
print("myValue = \(ov!)")
} else {
print("Object does not respond to the selector \"myValue\".")
}
}
// CHECK: X.f()
test_dynamic_lookup_f(X())
// CHECK: Object does not respond to the selector "f"
test_dynamic_lookup_f(Y())
// CHECK: Z.f()
test_dynamic_lookup_f(Z())
// CHECK-NEXT: (AnyObject) -> Optional<() -> ()>
print(type(of: AnyObject.f))
// CHECK-NEXT: X.f()
test_dynamic_lookup_f_unbound(X())
// CHECK-NEXT: Y does not respond to the selector "f"
test_dynamic_lookup_f_unbound(Y())
// CHECK-NEXT: Z.f()
test_dynamic_lookup_f_unbound(Z())
// CHECK-NEXT: (AnyObject) -> Optional<() -> ()>
print(type(of: AnyObject.e))
// CHECK-NEXT: X.e()
test_dynamic_lookup_e_unbound(X())
// CHECK-NEXT: Y does not respond to the selector "e"
test_dynamic_lookup_e_unbound(Y())
// CHECK-NEXT: Z does not respond to the selector "e"
test_dynamic_lookup_e_unbound(Z())
// CHECK: Class does not respond to the selector "g"
test_dynamic_lookup_g(X())
// CHECK: Y.g()
test_dynamic_lookup_g(Y())
// CHECK: X.myValue getter
// CHECK: myValue = 17
test_dynamic_lookup_myValue(X())
// CHECK: Object does not respond to the selector "myValue"
test_dynamic_lookup_myValue(Y())
// <rdar://problem/16554056> __FUNCTION__ in deinit for NSObject subclasses crashes the compiler
// Test __FUNCTION__
class FUNCTION_NAME_TEST : NSObject {
override init() { super.init() ; print(#function) }
deinit { print(#function) }
}
FUNCTION_NAME_TEST()
// CHECK: init()
// CHECK: deinit