/*
Mutating Getter & Nonmutating Setter
https://gist.github.com/hanawat/26f64afba4e6461dc27c
*/
// Mutating Getter
struct Reference {
private let _value: Double // inner value of this instance
var count = 0 // times this inner value has been referenced.
init(value: Double) { _value = value }
var value: Double {
mutating get { // ⭐️ mutating getter
count += 1 // mutates `count` property
return _value
}
}
}
var ref = Reference(value: 0.125)
for _ in 0..<5 { print(ref.value) } // reference its value 5 times.
print(ref.count) // 5
// Nonmutating Setter
struct Independent {
private static var _pool = [Double]() // pool of values for this type
let index: Int // instance index
init(value: Double) {
index = Independent._pool.count
Independent._pool.append(value)
}
// instance method
var value: Double {
// ⭐️ 注意:
// 這個物件的設計從來沒有把 _pool 裡面的東西刪除,
// 否則這裡的 _pool[index] 可能會出問題。
get { return Independent._pool[index] }
// ⭐️ nonmutating setter
// instance property `index` is NOT mutated.
// only TYPE property `_pool` is mutated.
nonmutating set { Independent._pool[index] = newValue }
}
// ⭐️ type method
static func clear() {
_pool = _pool.flatMap { _ in 0.0 }
}
}
// ⭐️ this is a `let`
let a0 = Independent(value: 0.125)
print(a0.value) // 0.125
a0.value = 5.25 // ⭐️ but its value can be changed.
print(a0.value) // 5.25
Independent.clear()
print(a0.value) // 0.0