Packages and Modules
Access Boundaries
Access control keeps stored details behind public methods and properties.
Hide state behind methods
access_boundaries.swift
Replay: real traced execution (multi-file project)
struct ScoreBox {
private var value: Int
init(start: Int) {
value = start
}
mutating func add(_ amount: Int) {
value += amount
}
var current: Int {
return value
}
}
let bonus = 4
var box = ScoreBox(start: 10)
box.add(bonus)
let message = "score=\(box.current)"
print(message)
struct ScoreBox {
private var value: Int
init(start: Int) {
value = start
}
mutating func add(_ amount: Int) {
value += amount
}
var current: Int {
return value
}
}
let bonus = 0
var box = ScoreBox(start: 10)
box.add(bonus)
let message = "score=\(box.current)"
print(message)
struct ScoreBox {
private var value: Int
init(start: Int) {
value = start
}
mutating func add(_ amount: Int) {
value += amount
}
var current: Int {
return value
}
}
let bonus = 9
var box = ScoreBox(start: 10)
box.add(bonus)
let message = "score=\(box.current)"
print(message)
bonus ← 4, value ← 10, box ← ScoreBox(value: 10)
4 init(start: Int) {5 value→ 10 = start106 }78 mutating func add(_ amount: Int) {9 value += amount10 }1112 var current: Int {13 return value14 }15}1617let bonus→ 4 = 4 //@bonus=0, 918var box→ ScoreBox(value: 10) = ScoreBox(start: 10)19boxScoreBox(value: 10).add(bonus4)20let message = "score=\(box.current)"value ← 14
8mutating func add(_ amount4: Int) {9 value→ 14 += amount410}box ← ScoreBox(value: 14), message ← score=14
12 var current: Int {13 return value1414 }15}1617let bonus = 4 //@bonus=0, 918var box = ScoreBox(start: 10)19box→ ScoreBox(value: 14).add(bonus4)20let message→ score=14 = "score=\(box.current14)"2122print(messagescore=14)outputscore=14
bonus ← 0, value ← 10, box ← ScoreBox(value: 10)
4 init(start: Int) {5 value→ 10 = start106 }78 mutating func add(_ amount: Int) {9 value += amount10 }1112 var current: Int {13 return value14 }15}1617let bonus→ 0 = 018var box→ ScoreBox(value: 10) = ScoreBox(start: 10)19boxScoreBox(value: 10).add(bonus0)20let message = "score=\(box.current)"mutating func add(_ amount: Int)
8mutating func add(_ amount0: Int) {9 value10 += amount010}message ← score=10
12 var current: Int {13 return value1014 }15}1617let bonus = 018var box = ScoreBox(start: 10)19boxScoreBox(value: 10).add(bonus0)20let message→ score=10 = "score=\(box.current10)"2122print(messagescore=10)outputscore=10
bonus ← 9, value ← 10, box ← ScoreBox(value: 10)
4 init(start: Int) {5 value→ 10 = start106 }78 mutating func add(_ amount: Int) {9 value += amount10 }1112 var current: Int {13 return value14 }15}1617let bonus→ 9 = 918var box→ ScoreBox(value: 10) = ScoreBox(start: 10)19boxScoreBox(value: 10).add(bonus9)20let message = "score=\(box.current)"value ← 19
8mutating func add(_ amount9: Int) {9 value→ 19 += amount910}box ← ScoreBox(value: 19), message ← score=19
12 var current: Int {13 return value1914 }15}1617let bonus = 918var box = ScoreBox(start: 10)19box→ ScoreBox(value: 19).add(bonus9)20let message→ score=19 = "score=\(box.current19)"2122print(messagescore=19)outputscore=19
access control
`private` keeps implementation details inside a type while methods expose the operations callers are allowed to perform.