-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathprotocols.swift
172 lines (138 loc) · 4.91 KB
/
protocols.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
// RUN: %swift -parse %s -verify
protocol MyPrintable {
func print()
}
protocol Titled {
var title : String { get set }
}
struct IsPrintable1 : FormattedPrintable, Titled, Document {
var title = ""
func print() {}
func print(_: TestFormat) {}
}
// Printability is below
struct IsPrintable2 { }
struct IsNotPrintable1 { }
struct IsNotPrintable2 {
func print(_: Int) -> Int {}
}
struct Book : Titled {
var title : String
}
struct Lackey : Titled {
var title : String {
get {}
set {}
}
}
struct Number {
var title : Int
}
func testPrintableCoercion(ip1: IsPrintable1,
ip2: IsPrintable2,
inp1: IsNotPrintable1,
inp2: IsNotPrintable2,
op: OtherPrintable) {
var p : MyPrintable = ip1 // okay
p = ip1 // okay
p = ip2 // okay
p = inp1 // expected-error{{cannot assign a value of type 'IsNotPrintable1' to a value of type 'MyPrintable'}}
p = inp2 // expected-error{{cannot assign a value of type 'IsNotPrintable2' to a value of type 'MyPrintable'}}
p = op // expected-error{{cannot assign a value of type 'OtherPrintable' to a value of type 'MyPrintable'}}
}
func testTitledCoercion(ip1: IsPrintable1, book: Book, lackey: Lackey,
number: Number, ip2: IsPrintable2) {
var t : Titled = ip1 // okay
t = ip1
t = book
t = lackey
t = number // expected-error{{cannot assign a value of type 'Number' to a value of type 'Titled'}}
t = ip2 // expected-error{{cannot assign a value of type 'IsPrintable2' to a value of type 'Titled'}}
}
extension IsPrintable2 : MyPrintable {
func print() {}
}
protocol OtherPrintable {
func print()
}
struct TestFormat {}
protocol FormattedPrintable : MyPrintable {
func print(_: TestFormat)
}
struct NotFormattedPrintable1 {
func print(_: TestFormat) { }
}
func testFormattedPrintableCoercion(ip1: IsPrintable1,
ip2: IsPrintable2,
inout fp: FormattedPrintable,
inout p: MyPrintable,
inout op: OtherPrintable,
nfp1: NotFormattedPrintable1) {
fp = ip1
fp = ip2 // expected-error{{cannot assign a value of type 'IsPrintable2' to a value of type 'FormattedPrintable'}}
fp = nfp1 // expected-error{{cannot assign a value of type 'NotFormattedPrintable1' to a value of type 'FormattedPrintable'}}
p = fp
op = fp // expected-error{{cannot assign a value of type 'FormattedPrintable' to a value of type 'OtherPrintable'}}
fp = op // expected-error{{cannot assign a value of type 'OtherPrintable' to a value of type 'FormattedPrintable'}}
}
protocol Document : Titled, MyPrintable {
}
func testMethodsAndVars(fp: FormattedPrintable, f: TestFormat, inout doc: Document) {
fp.print(f)
fp.print()
doc.title = "Gone with the Wind"
doc.print()
}
func testDocumentCoercion(inout doc: Document, ip1: IsPrintable1, l: Lackey) {
doc = ip1
doc = l // expected-error{{cannot assign a value of type 'Lackey' to a value of type 'Document'}}
}
// Check coercion of references.
func refCoercion(inout p: MyPrintable) { }
var p : MyPrintable = IsPrintable1()
var fp : FormattedPrintable = IsPrintable1()
var ip1 : IsPrintable1
refCoercion(&p)
refCoercion(&fp) // expected-error{{cannot invoke 'refCoercion' with an argument list of type '(inout FormattedPrintable)'}} expected-note{{expected an argument list of type '(inout MyPrintable)'}}
refCoercion(&ip1) // expected-error{{cannot invoke 'refCoercion' with an argument list of type '(inout IsPrintable1)'}} expected-note{{expected an argument list of type '(inout MyPrintable)'}}
protocol IntSubscriptable {
subscript(i: Int) -> Int { get }
}
struct IsIntSubscriptable : IntSubscriptable {
subscript(i: Int) -> Int { get {} set {} }
}
struct IsDoubleSubscriptable {
subscript(d: Double) -> Int { get {} set {} }
}
struct IsIntToStringSubscriptable {
subscript(i: Int) -> String { get {} set {} }
}
func testIntSubscripting(inout i_s: IntSubscriptable,
iis: IsIntSubscriptable,
ids: IsDoubleSubscriptable,
iiss: IsIntToStringSubscriptable) {
var x = i_s[17]
i_s[5] = 7 // expected-error{{cannot assign to the result of this expression}}
i_s = iis
i_s = ids // expected-error{{cannot assign a value of type 'IsDoubleSubscriptable' to a value of type 'IntSubscriptable'}}
i_s = iiss // expected-error{{cannot assign a value of type 'IsIntToStringSubscriptable' to a value of type 'IntSubscriptable'}}
}
protocol MyREPLPrintable {
func myReplPrint()
}
extension Int : MyREPLPrintable {
func myReplPrint() {}
}
extension String : MyREPLPrintable {
func myReplPrint() {}
}
func doREPLPrint(p: MyREPLPrintable) {
p.myReplPrint()
}
func testREPLPrintable() {
var i : Int
var rp : MyREPLPrintable = i
doREPLPrint(i)
doREPLPrint(1)
doREPLPrint("foo")
}