-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathgeneric_subscript_static.swift
197 lines (158 loc) · 4.39 KB
/
generic_subscript_static.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
190
191
192
193
194
195
196
197
//===--- generic_subscript.swift ------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
//
import StdlibUnittest
var GenericSubscriptTestSuite = TestSuite("GenericSubscriptStatic")
var ts: [ObjectIdentifier: Any] = [:]
struct S<T> : P {
typealias Element = T
static var t: T {
get {
return ts[ObjectIdentifier(self)] as! T
}
set {
ts[ObjectIdentifier(self)] = newValue
}
}
static subscript<U>(a: (T) -> U, b: (U) -> T) -> U {
get {
print(T.self)
print(U.self)
return a(t)
}
set {
print(T.self)
print(U.self)
t = b(newValue)
}
}
}
protocol P {
associatedtype Element
static subscript<U>(a: (Element) -> U, b: (U) -> Element) -> U { get set }
}
func increment<T : P>(p: T.Type) where T.Element == String {
p[{Int($0)!}, {String($0)}] += 1
}
GenericSubscriptTestSuite.test("Basic") {
var s = S<String>.self
s.t = "0"
increment(p: s)
expectEqual(s.t, "1")
}
protocol AnySubscript {
static subscript(k: AnyHashable) -> Any? { get set }
}
struct AnyDictionary : AnySubscript {
static var dict: [AnyHashable : Any] = [:]
static subscript(k: AnyHashable) -> Any? {
get {
return dict[k]
}
set {
dict[k] = newValue
}
}
}
extension AnySubscript {
static subscript<K : Hashable, V>(k k: K) -> V? {
get {
return self[k] as! V?
}
set {
self[k] = newValue
}
}
}
GenericSubscriptTestSuite.test("ProtocolExtensionConcrete") {
var dict = AnyDictionary.self
func doIt(dict: AnyDictionary.Type) {
dict["a" ] = 0
dict[k: "a"]! += 1
}
doIt(dict: dict)
expectEqual(dict["a"]! as! Int, 1)
expectEqual(dict[k: "a"]!, 1)
}
GenericSubscriptTestSuite.test("ProtocolExtensionAbstract") {
var dict = AnyDictionary.self
func doIt<T : AnySubscript>(dict: T.Type) {
dict["a" ] = 0
dict[k: "a"]! += 1
}
doIt(dict: dict)
expectEqual(dict["a"]! as! Int, 1)
expectEqual(dict[k: "a"]!, 1)
}
protocol GenericSubscript : AnySubscript {
static subscript<K : Hashable, V>(k k: K) -> V? { get set }
}
extension AnyDictionary : GenericSubscript { }
GenericSubscriptTestSuite.test("ProtocolExtensionWitness") {
var dict = AnyDictionary.self
func doIt<T : GenericSubscript>(dict: T.Type) {
dict["a" ] = 0
dict[k: "a"]! += 1
}
doIt(dict: dict)
expectEqual(dict["a"]! as! Int, 1)
expectEqual(dict[k: "a"]!, 1)
}
class EchoBase<T: SignedNumeric> {
// In EchoDerived, subscript(a:) will be overridden.
class subscript(a a: T) -> String {
get {
return "EchoBase.Type.subscript(a: \(a))"
}
}
// In EchoDerived, subscript(b:) will be overridden with a super call.
class subscript(b b: T) -> String {
get {
return "EchoBase.Type.subscript(b: \(b))"
}
}
// In EchoDerived, subscript(c:) will not be overridden.
class subscript(c c: T) -> String {
get {
return "EchoBase.Type.subscript(c: \(c))"
}
}
}
class EchoDerived<T: SignedNumeric>: EchoBase<T> {
override class subscript(a a: T) -> String {
get {
return "EchoDerived.Type.subscript(a: \(a))"
}
}
override class subscript(b b: T) -> String {
get {
return "\(super[b: -b]) EchoDerived.Type.subscript(b: \(b))"
}
}
class subscript(d d: T) -> String {
return "EchoDerived.Type.subscript(d: \(d))"
}
}
GenericSubscriptTestSuite.test("Overrides") {
let base = EchoBase<Int>.self
let derived = EchoDerived<Int>.self
expectEqual(base[a: 42], "EchoBase.Type.subscript(a: 42)")
expectEqual(base[b: 42], "EchoBase.Type.subscript(b: 42)")
expectEqual(base[c: 42], "EchoBase.Type.subscript(c: 42)")
expectEqual(derived[a: 42], "EchoDerived.Type.subscript(a: 42)")
expectEqual(derived[b: 42], "EchoBase.Type.subscript(b: -42) EchoDerived.Type.subscript(b: 42)")
expectEqual(derived[c: 42], "EchoBase.Type.subscript(c: 42)")
expectEqual(derived[d: 42], "EchoDerived.Type.subscript(d: 42)")
}
runAllTests()