File: def_struct.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 (139 lines) | stat: -rw-r--r-- 2,489 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
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 = Resettable & Computable

public protocol SpecialResettable : Resettable, Computable {}

public protocol HasAssociatedType {
  associatedtype ComputableType : Computable
}

public struct ComputableWrapper<T : Computable> : HasAssociatedType {
  public typealias ComputableType = T
  public init() {}
}

public protocol AnotherAssociated {
  associatedtype ResettableType : Resettable
}

public struct ResettableWrapper<T : Resettable> : AnotherAssociated {
  public typealias ResettableType = T
  public init() {}
}

public func cacheViaWrappers<
  T : HasAssociatedType, U : AnotherAssociated
>(_ computable : T, _ resettable : U)
  where T.ComputableType == U.ResettableType {}


// 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
}