forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdef_struct.swift
139 lines (113 loc) · 2.44 KB
/
def_struct.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
public struct Empty {}
public struct TwoInts {
public var x, y : Int
public init(x: Int, y: Int) {
self.x = x
self.y = y
}
}
public struct ComputedProperty {
public var value : Int {
get {
var result = 0
return result
}
}
}
public struct StaticProperties {
public static var foo: Int = 0
public static let bar: Int = 0
public static var baz: Int {
return 0
}
}
// Generics
public struct Pair<A, B> {
public var first : A
public var second : B
public init(a : A, b : B) {
first = a
second = b
}
}
public typealias VoidPairTuple = ((), ())
public struct GenericCtor<U> {
public init<T>(_ t : T) {}
public func doSomething<T>(t: T) {}
}
// Protocols
public protocol Resettable {
mutating
func reset()
}
public struct ResettableIntWrapper : Resettable {
public var value : Int
public mutating
func reset() {
var zero = 0
value = zero
}
public init(value: Int) { self.value = value }
}
public protocol Computable {
mutating
func compute()
}
public typealias Cacheable = protocol<Resettable, Computable>
public protocol SpecialResettable : Resettable, Computable {}
public protocol HasAssociatedType {
typealias ComputableType : Computable
}
public struct ComputableWrapper<T : Computable> : HasAssociatedType {
public typealias ComputableType = T
public init() {}
}
public protocol AnotherAssociatedType {
typealias ResettableType : Resettable
}
public struct ResettableWrapper<T : Resettable> : AnotherAssociatedType {
public typealias ResettableType = T
public init() {}
}
public func cacheViaWrappers<
T : HasAssociatedType, U : AnotherAssociatedType
where T.ComputableType == U.ResettableType
>(computable : T, _ resettable : U) {}
// Subscripts
public struct ReadonlySimpleSubscript {
public subscript(x : Int) -> Bool {
return true
}
public init() {}
}
public struct ComplexSubscript {
public subscript(x : Int, y : Bool) -> Int {
set(newValue) {
// do nothing!
}
get {
return 0
}
}
public init() {}
}
// Extensions
public extension Empty {
public func doAbsolutelyNothing() {}
}
public struct UnComputable {}
extension UnComputable : Computable {
public init(x : Int) {}
public func compute() {}
public static func canCompute() -> Bool {
return true
}
}
public extension Pair {
public func swap() -> (B, A) {
return (second, first)
}
}
public struct Burger {
public let pattyCount: Int
}