Repository navigation
Expand file tree
/
Copy pathStructsAndClasses.swift
More file actions
102 lines (82 loc) · 2.56 KB
/
Copy pathStructsAndClasses.swift
File metadata and controls
102 lines (82 loc) · 2.56 KB
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
// A struct is a value type, copied on assignment; a class is a reference
// type, shared. Choosing between them is most of Swift's design work.
struct PointValue {
var x: Double
var y: Double
// Changing a property of a struct needs `mutating`.
mutating func move(dx: Double, dy: Double) {
x += dx
y += dy
}
// A non-mutating alternative returns a new value.
func moved(dx: Double, dy: Double) -> PointValue {
PointValue(x: x + dx, y: y + dy)
}
}
final class PointReference {
var x: Double
var y: Double
init(x: Double, y: Double) {
self.x = x
self.y = y
}
func move(dx: Double, dy: Double) {
x += dx
y += dy
}
deinit {
print(" a PointReference was released")
}
}
class Vehicle {
let name: String
private(set) var capacity: Int
init(name: String, capacity: Int) {
self.name = name
self.capacity = capacity
}
func describe() -> String {
"\(name) carries \(capacity)"
}
}
final class Tram: Vehicle {
let line: String
private var charge = 100
init(name: String, capacity: Int, line: String) {
self.line = line
super.init(name: name, capacity: capacity) // after the subclass's own properties
}
func drain(_ amount: Int) {
charge = max(0, charge - amount)
}
override func describe() -> String {
"\(super.describe()) on the \(line) line, \(charge)% charged"
}
}
// Structs get a memberwise initialiser for free.
var original = PointValue(x: 1, y: 2)
var copy = original
copy.move(dx: 100, dy: 0)
print("value semantics: original \(original.x), copy \(copy.x)")
print("non-mutating:", original.moved(dx: 5, dy: 5))
let shared = PointReference(x: 1, y: 2)
let alias = shared
alias.move(dx: 100, dy: 0)
print("reference semantics: both read \(shared.x)")
// let on a struct freezes its properties; on a class only the binding.
let frozen = PointValue(x: 0, y: 0)
// frozen.x = 1 // would not compile
let constantReference = PointReference(x: 0, y: 0)
constantReference.x = 1 // allowed: the object is mutable, the binding is not
print("class through a let:", constantReference.x)
let tram = Tram(name: "Tram 14", capacity: 180, line: "Amber")
tram.drain(35)
print(tram.describe())
print("is a Vehicle:", tram is Vehicle)
// Identity applies only to references.
print("identical:", shared === alias, "| different objects:", shared === constantReference)
// deinit runs when the last reference goes away.
do {
_ = PointReference(x: 9, y: 9)
}
print("done")