示例工程 06-functions-closures
06-functions-closures/Sources/Ch06Crash/App.swift
import Foundation
final class Counter {
var value = 0
}
/// 先以 `inout` 独占 `value`,再调用闭包;闭包又修改同一个属性。
@inline(never)
func bump(_ value: inout Int, then body: () -> Void) {
value += 1
body()
print("after: \(value)")
}
@main
struct Ch06Crash {
static func main() {
let counter = Counter()
FileHandle.standardError.write(Data("about to overlap access\n".utf8))
bump(&counter.value) {
counter.value += 1
}
print("unreachable \(counter.value)")
}
}