File: protocol_extensions.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (397 lines) | stat: -rw-r--r-- 9,137 bytes parent folder | download
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// RUN: %target-run-simple-swift
// REQUIRES: executable_test

import StdlibUnittest

defer { runAllTests() }

var ProtocolExtensionTestSuite = TestSuite("ProtocolExtensions")

// Extend a protocol with a property.
extension Sequence {
  var myCount: Int {
    var result = 0
    for _ in self {
      result += 1
    }
    return result
  }
}

// Extend a protocol with a function.
extension Collection {
  var myIndices: Range<Index> {
    return startIndex..<endIndex
  }

  func clone() -> Self {
    return self
  }
}

ProtocolExtensionTestSuite.test("Count") {
  expectEqual(4, ["a", "b", "c", "d"].myCount)
  expectEqual(4, ["a", "b", "c", "d"].clone().myCount)
}

extension Sequence {
  public func myEnumerated() -> EnumeratedSequence<Self> {
    return self.enumerated()
  }
}

ProtocolExtensionTestSuite.test("Enumerated") {
  var result: [(Int, String)] = []

  for (index, element) in ["a", "b", "c"].myEnumerated() {
    result.append((index, element))
  }

  expectTrue((0, "a") == result[0])
  expectTrue((1, "b") == result[1])
  expectTrue((2, "c") == result[2])
}

extension Sequence {
  public func myReduce<T>(
    _ initial: T, combine: (T, Self.Iterator.Element) -> T
  ) -> T {
    var result = initial
    for value in self {
      result = combine(result, value)
    }
    return result
  }
}

extension Sequence {
  public func myZip<S : Sequence>(_ s: S) -> Zip2Sequence<Self, S> {
    return zip(self, s)
  }
}

ProtocolExtensionTestSuite.test("Algorithms") {
  expectEqual(15, [1, 2, 3, 4, 5].myReduce(0, combine: { $0 + $1 }))

  var result: [(Int, String)] = []

  for (a, b) in [1, 2, 3].myZip(["a", "b", "c"]) {
    result.append((a, b))
  }

  expectTrue((1, "a") == result[0])
  expectTrue((2, "b") == result[1])
  expectTrue((3, "c") == result[2])
}

// Mutating algorithms.
extension MutableCollection
  where Self: RandomAccessCollection, Self.Iterator.Element : Comparable {

  public mutating func myPartition() -> Index {
    let first = self.first
    return self.partition(by: { $0 >= first! })
  }
}

extension RangeReplaceableCollection {
  public func myJoin<S : Sequence>(
    _ elements: S
  ) -> Self where S.Iterator.Element == Self {
    var result = Self()
    var iter = elements.makeIterator()
    if let first = iter.next() {
      result.append(contentsOf: first)
      while let next = iter.next() {
        result.append(contentsOf: self)
        result.append(contentsOf: next)
      }
    }
    return result
  }
}

ProtocolExtensionTestSuite.test("MutatingAlgorithms") {
  expectEqual("a,b,c", ",".myJoin(["a", "b", "c"]))
}

// Constrained extensions for specific types.
extension Collection where Self.Iterator.Element == String {
  var myCommaSeparatedList: String {
    if startIndex == endIndex { return "" }

    var result = ""
    var first = true
    for x in self {
      if first { first = false }
      else { result += ", " }
      result += x
    }
    return result
  }
}

ProtocolExtensionTestSuite.test("ConstrainedExtension") {
  expectEqual("x, y, z", ["x", "y", "z"].myCommaSeparatedList)
}

// Existentials
var runExistP1 = 0
var existP1_struct = 0
var existP1_class = 0

protocol ExistP1 {
  func existP1()
}

extension ExistP1 {
  func runExistP1() {
    main.runExistP1 += 1
    self.existP1()
  }
}

struct ExistP1_Struct : ExistP1 {
  func existP1() {
    existP1_struct += 1
  }
}

class ExistP1_Class : ExistP1 {
  func existP1() {
    existP1_class += 1
  }
}

ProtocolExtensionTestSuite.test("Existentials") {
  do {
    let existP1: ExistP1 = ExistP1_Struct()
    existP1.runExistP1()

    expectEqual(1, runExistP1)
    expectEqual(1, existP1_struct)
  }

  do {
    let existP1 = ExistP1_Class()
    existP1.runExistP1()

    expectEqual(2, runExistP1)
    expectEqual(1, existP1_class)
  }
}

protocol P {
  mutating func setValue(_ b: Bool)
  func getValue() -> Bool
}

extension P {
  var extValue: Bool {
    get { return getValue() }
    set(newValue) { setValue(newValue) }
  }
}

extension Bool : P {
  mutating func setValue(_ b: Bool) { self = b }
  func getValue() -> Bool { return self }
}

class C : P {
  var theValue: Bool = false
  func setValue(_ b: Bool) { theValue = b }
  func getValue() -> Bool { return theValue }
}

func toggle(_ value: inout Bool) {
  value = !value
}

ProtocolExtensionTestSuite.test("ExistentialToggle") {
  var p: P = true

  expectTrue(p.extValue)

  p.extValue = false
  expectFalse(p.extValue)

  toggle(&p.extValue)
  expectTrue(p.extValue)

  p = C()

  p.extValue = true
  expectTrue(p.extValue)

  p.extValue = false
  expectFalse(p.extValue)

  toggle(&p.extValue)
  expectTrue(p.extValue)
}

// Logical lvalues of existential type.
struct HasP {
  var _p: P
  var p: P {
    get { return _p }
    set { _p = newValue }
  }
}

ProtocolExtensionTestSuite.test("ExistentialLValue") {
  var hasP = HasP(_p: false)

  hasP.p.extValue = true
  expectTrue(hasP.p.extValue)

  toggle(&hasP.p.extValue)
  expectFalse(hasP.p.extValue)
}

var metatypes: [(Int, Any.Type)] = []

// rdar://problem/20739719
class Super: Init {
  required init(x: Int) {
    metatypes.append((x, type(of: self)))
  }
}

class Sub: Super {}

protocol Init { init(x: Int) }
extension Init { init() { self.init(x: 17) } }

ProtocolExtensionTestSuite.test("ClassInitializer") {
  _ = Super()

  _ = Sub()

  var sup: Super.Type = Super.self
  _ = sup.init()

  sup = Sub.self
  _ = sup.init()

  expectTrue(17 == metatypes[0].0)
  expectTrue(Super.self == metatypes[0].1)
  expectTrue(17 == metatypes[1].0)
  expectTrue(Sub.self == metatypes[1].1)
  expectTrue(17 == metatypes[2].0)
  expectTrue(Super.self == metatypes[2].1)
  expectTrue(17 == metatypes[3].0)
  expectTrue(Sub.self == metatypes[3].1)
}

// https://github.com/apple/swift/issues/43234

protocol SelfMetadataTest {
  associatedtype T = Int

  func staticTypeOfSelf() -> Any.Type
  func staticTypeOfSelfTakesT(_: T) -> Any.Type
  func staticTypeOfSelfCallsWitness() -> Any.Type
}

extension SelfMetadataTest {
  func staticTypeOfSelf() -> Any.Type {
    return Self.self
  }

  func staticTypeOfSelfTakesT(_: T) -> Any.Type {
    return Self.self
  }

  func staticTypeOfSelfNotAWitness() -> Any.Type {
    return Self.self
  }

  func staticTypeOfSelfCallsWitness() -> Any.Type {
    return staticTypeOfSelf()
  }
}

class SelfMetadataBase : SelfMetadataTest {}

class SelfMetadataDerived : SelfMetadataBase {}

class SelfMetadataGeneric<T> : SelfMetadataTest {}

func testSelfMetadata<T : SelfMetadataTest>(_ x: T, _ t: T.T) -> [Any.Type] {
  return [x.staticTypeOfSelf(),
          x.staticTypeOfSelfTakesT(t),
          x.staticTypeOfSelfNotAWitness(),
          x.staticTypeOfSelfCallsWitness()]
}

ProtocolExtensionTestSuite.test("WitnessSelf") {
  do {
    let result = testSelfMetadata(SelfMetadataBase(), 0)
    expectTrue(SelfMetadataBase.self == result[0])
    expectTrue(SelfMetadataBase.self == result[1])
    expectTrue(SelfMetadataBase.self == result[2])
    expectTrue(SelfMetadataBase.self == result[3])
  }

  do {
    let result = testSelfMetadata(SelfMetadataDerived() as SelfMetadataBase, 0)
    expectTrue(SelfMetadataBase.self == result[0])
    expectTrue(SelfMetadataBase.self == result[1])
    expectTrue(SelfMetadataBase.self == result[2])
    expectTrue(SelfMetadataBase.self == result[3])
  }

  // This is the interesting case -- make sure the static type of 'Self'
  // is correctly passed on from the call site to the extension method
  do {
    let result = testSelfMetadata(SelfMetadataDerived(), 0)
    expectTrue(SelfMetadataDerived.self == result[0])
    expectTrue(SelfMetadataBase.self == result[1])
    expectTrue(SelfMetadataDerived.self == result[2])
    expectTrue(SelfMetadataDerived.self == result[3])
  }

  // Make sure the calling convention works out if 'Self' is a generic
  // class too.
  do {
    let result = testSelfMetadata(SelfMetadataGeneric<Int>(), 0)
    expectTrue(SelfMetadataGeneric<Int>.self == result[0])
    expectTrue(SelfMetadataGeneric<Int>.self == result[1])
    expectTrue(SelfMetadataGeneric<Int>.self == result[2])
    expectTrue(SelfMetadataGeneric<Int>.self == result[3])
  }
}

@_marker protocol Addable {}
extension Addable {
    func increment(this x: Int) -> Int { return x + 100 }
}
extension String: Addable {}

ProtocolExtensionTestSuite.test("MarkerProtocolExtensions") {
    expectTrue("hello".increment(this: 11) == 111)
}

protocol DefaultArgumentsInExtension {
    func foo(a: Int, b: Int) -> (Int, Int)
}
extension DefaultArgumentsInExtension {
    func foo(a: Int, b: Int = 1) -> (Int, Int) {
        self.foo(a: a * 10, b: b * 10)
    }
}
struct DefaultArgumentsInExtensionImpl: DefaultArgumentsInExtension {
    func foo(a: Int, b: Int) -> (Int, Int) {
        (a * 2, b * 2)
    }
}

ProtocolExtensionTestSuite.test("DefaultArgumentsInExtension") {
    let instance = DefaultArgumentsInExtensionImpl()
    expectEqual((4, 6), instance.foo(a: 2, b: 3))
    expectEqual((4, 6), (instance as any DefaultArgumentsInExtension).foo(a: 2, b: 3))
    expectEqual((40, 20), instance.foo(a: 2))
    expectEqual((40, 20), (instance as any DefaultArgumentsInExtension).foo(a: 2))
}