forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdef_class.swift
185 lines (142 loc) · 3.25 KB
/
def_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
open class Empty {}
open class TwoInts {
open var x, y : Int
required public init(a : Int, b : Int) {
x = a
y = b
}
}
open class ComputedProperty {
open var value : Int {
get {
var result = 0
return result
}
set(newVal) {
// completely ignore it!
}
}
open var readOnly : Int {
return 42
}
public init() {}
}
// Generics
open class Pair<A, B> {
open var first : A
open var second : B
public init(a : A, b : B) {
first = a
second = b
}
}
open class GenericCtor<U> {
public init<T>(_ t : T) {}
open func doSomething<T>(_ t : T) {}
}
// Protocols
public protocol Resettable {
func reset()
}
public extension Resettable {
func doReset() { self.reset() }
}
open class ResettableIntWrapper : Resettable {
open var value : Int
public init() { value = 0 }
open func reset() {
var zero = 0
value = zero
}
}
public protocol Computable {
func compute()
}
public typealias Cacheable = Resettable & Computable
public protocol SpecialResettable : Resettable, Computable {}
public protocol PairLike {
associatedtype FirstType
associatedtype SecondType
func getFirst() -> FirstType
func getSecond() -> SecondType
}
public extension PairLike where FirstType : Cacheable {
func cacheFirst() { }
}
public protocol ClassProto : class {}
@objc public protocol ObjCProtoWithOptional {
@objc optional func optionalMethod()
@objc optional var optionalVar: Int { get }
@objc optional subscript (i: Int) -> Int { get }
}
open class OptionalImplementer : ObjCProtoWithOptional {
open func unrelated() {}
public init() {}
}
// Inheritance
open class StillEmpty : Empty, Resettable {
open func reset() {}
public override init() {}
}
open class BoolPair<T> : Pair<Bool, Bool>, PairLike {
public init() { super.init(a: false, b: false) }
open func bothTrue() -> Bool {
return first && second
}
open func getFirst() -> Bool { return first }
open func getSecond() -> Bool { return second }
}
open class SpecialPair<A> : Pair<Int, Int>, Computable {
open func compute() {}
}
open class OtherPair<A, B> : PairLike {
open var first : A
open var second : B
public init(a : A, b : B) {
first = a
second = b
}
public typealias FirstType = Bool
public typealias SecondType = Bool
open func getFirst() -> Bool { return true }
open func getSecond() -> Bool { return true }
}
open class OtherBoolPair<T> : OtherPair<Bool, Bool> {
}
open class RequiresPairLike<P : PairLike> { }
public func getReqPairLike() -> RequiresPairLike<OtherBoolPair<Bool>> {
return RequiresPairLike<OtherBoolPair<Bool>>()
}
// Subscripts
open class ReadonlySimpleSubscript {
open subscript(x : Int) -> Bool {
return true
}
public init() {}
}
open class ComplexSubscript {
open subscript(x : Int, y : Bool) -> Int {
set(newValue) {
// do nothing!
}
get {
return 0
}
}
public init() {}
}
// Destructor
open class Resource {
public init() { }
deinit {}
}
// Ownership
open class ResourceSharer {
// FIXME: Cannot perform in-class initialization here
open unowned var alwaysPresent : Resource
open weak var maybePresent : Resource?
public init (res: Resource) {
self.alwaysPresent = res
self.maybePresent = nil
}
}