在Xcode 10.2+的惰性评估中,我注意到了不同的行为。
如果我拥有当前代码:
class C {
private let myString: String?
private let tag: Int
init(string: String?, tag: Int) {
myString = string
self.tag = tag
}
func f() -> String? {
print("\(tag) called")
return myString
}
}
let a = C(string: nil, tag: 0)
let b = C(string: "2", tag: 1)
let c = C(string: nil, tag: 2)
let array = [a, b, c]
let string = array.lazy.compactMap { $0.f() }.first
我期望输出:
0 called
1 called
(我在Xcode 10.1中拥有)
但实际上是:
0 called
1 called
1 called // called twice
我不会说这是Swift 5的问题,因为我的项目仍在使用Swift 4.0