forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeneric_class.swift
189 lines (160 loc) · 4.24 KB
/
generic_class.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
// RUN: rm -rf %t && mkdir %t
// RUN: %target-build-swift %s -o %t/a.out
// RUN: %target-run %t/a.out | FileCheck %s
// REQUIRES: executable_test
protocol MyPrintable {
func myPrint()
}
extension Int : MyPrintable {
func myPrint() {
print(self.description, terminator: "")
}
}
extension Double : MyPrintable {
func myPrint() {
print(self.description, terminator: "")
}
}
extension String : MyPrintable {
func myPrint() {
print(self.debugDescription, terminator: "")
}
}
class BufferedPair<T, U> {
var front: UInt8
var first: T
var second: U
var back: UInt8
init(_ front: UInt8, _ first: T, _ second: U, _ back: UInt8) {
self.front = front
self.first = first
self.second = second
self.back = back
}
}
enum State : MyPrintable {
case CA, OR, WA
func myPrint() {
switch self {
case .CA:
print("California", terminator: "")
case .OR:
print("Oregon", terminator: "")
case .WA:
print("Washington", terminator: "")
}
}
}
func printPair<A: MyPrintable, B: MyPrintable>(p: BufferedPair<A,B>) {
print("\(p.front) ", terminator: "")
p.first.myPrint()
print(" ", terminator: "")
p.second.myPrint()
print(" \(p.back)")
}
var p = BufferedPair(99, State.OR, "Washington's Mexico", 84)
// CHECK: 99 Oregon "Washington\'s Mexico" 84
printPair(p)
class AwkwardTriple<V, W, X> : BufferedPair<V, W> {
var third: X
init(_ front: UInt8, _ first: V, _ second: W, _ back: UInt8, _ third: X) {
self.third = third
super.init(front, first, second, back)
self.third = third
}
}
func printTriple
<D: MyPrintable, E: MyPrintable, F: MyPrintable>
(p: AwkwardTriple<D, E, F>)
{
print("\(p.front) ", terminator: "")
p.first.myPrint()
print(" ", terminator: "")
p.second.myPrint()
print(" \(p.back) ", terminator: "")
p.third.myPrint()
print("")
}
var q = AwkwardTriple(123, State.CA, "Foo", 234, State.WA)
// CHECK: 123 California "Foo" 234
printPair(q)
// CHECK: 123 California "Foo" 234 Washington
printTriple(q)
class FourthWheel<P, Q, R, S> : AwkwardTriple<P, Q, R> {
var fourth: S
init(_ front: UInt8, _ first: P, _ second: Q, _ back: UInt8, _ third: R,
_ fourth: S) {
self.fourth = fourth
super.init(front, first, second, back, third)
self.fourth = fourth
}
}
func printQuad
<G: MyPrintable, H: MyPrintable, I: MyPrintable, J: MyPrintable>
(p: FourthWheel<G, H, I, J>)
{
print("\(p.front) ", terminator: "")
p.first.myPrint()
print(" ", terminator: "")
p.second.myPrint()
print(" \(p.back) ", terminator: "")
p.third.myPrint()
print(" ", terminator: "")
p.fourth.myPrint()
print("")
}
var r = FourthWheel(21, State.WA, "Bar", 31, State.OR, 3.125)
// CHECK: 21 Washington "Bar" 31
printPair(r)
// CHECK: 21 Washington "Bar" 31 Oregon
printTriple(r)
var rAsPair: BufferedPair<State, String> = r
// CHECK: 21 Washington "Bar" 31 Oregon
printTriple(rAsPair as! AwkwardTriple<State, String, State>)
// CHECK: 21 Washington "Bar" 31 Oregon 3.125
printQuad(r)
// CHECK: 21 Washington "Bar" 31 Oregon 3.125
printQuad(rAsPair as! FourthWheel<State, String, State, Double>)
class ConcretePair {
var first, second: UInt8
init(_ first: UInt8, _ second: UInt8) {
self.first = first
self.second = second
}
}
class SemiConcreteTriple<O> : ConcretePair {
var third: O
init(_ first: UInt8, _ second: UInt8, _ third: O) {
self.third = third
super.init(first, second)
self.third = third
}
}
func printConcretePair(p: ConcretePair) {
print("\(p.first) \(p.second)")
}
func printSemiTriple<O : MyPrintable>(p: SemiConcreteTriple<O>) {
print("\(p.first) \(p.second) ", terminator: "")
p.third.myPrint()
print("")
}
var s = SemiConcreteTriple(120, 230, State.CA)
// CHECK: 120 230
printConcretePair(s)
// CHECK: 120 230 California
printSemiTriple(s)
var t = SemiConcreteTriple(121, 231, "California's Canada")
// CHECK: 121 231
printConcretePair(t)
// CHECK: 121 231 "California\'s Canada"
printSemiTriple(t)
class MoreConcreteQuadruple : SemiConcreteTriple<State> {
var fourth: String
init(_ first: UInt8, _ second: UInt8, _ third: State, _ fourth: String) {
self.fourth = fourth
super.init(first, second, third)
}
}
var u = MoreConcreteQuadruple(10, 17, State.CA, "Hella")
// CHECK: 10 17
printConcretePair(u)