如何在 Swift 中的变异结构上正确创建惰性派生属性



我正在制作一个具有非常昂贵的计算派生值的变异结构。因此,我想做的是延迟计算此派生值并存储结果,直到结构再次发生突变,此时派生值不再有效,需要重新计算。

(失败)选项 1:生成的属性

如果派生值是生成的属性(如下所示),则始终返回正确的值,但始终重新计算。

(失败)选项 2:延迟加载属性

如果它是一个惰性属性,则计算只执行一次...曾。因此,一旦结构发生变化,派生值就是错误的,不会重新计算。此外,如果我从结构中分配常量值,我无法访问该属性。

Swift 1.2 中是否有任何可能的解决方案,或者我需要提交雷达?

struct Struct {
    var value: Int
    // Option 1: Generated property
    var derivedValue: Int {
        println("Doing expensive calculation")
        return self.value * 2
    }
    // Option 2: Lazy property
    lazy var derivedValue: Int = {
        println("Doing expensive calculation")
        return self.value * 2
    }()
    init(value: Int) {
        self.value = value
    }
    mutating func mutate() {
        value = random()
    }
}
var test = Struct(value: 2)
test.derivedValue
test.derivedValue // If not lazy, expensive calculation is done again here
test.mutate()
test.derivedValue // If lazy, this has wrong value
let test2 = test
test2.derivedValue // Compiler error if using lazy implementation

使用嵌入式类可以绕过改变结构的限制。这允许您使用按值类型,该类型在需要之前不会运行昂贵的计算,但之后仍会记住结果。

下面的示例Number结构体计算并记住其 square 属性,其行为方式与您描述的方式相同。数学本身效率低得离谱,但它是说明解决方案的简单方法。

struct Number {
    // Store a cache in a nested class.
    // The struct only contains a reference to the class, not the class itself,
    // so the struct cannot prevent the class from mutating.
    private class Cache {
        var square: Int?
        var multiples: [Int: Int] = [:]
    }
    private var cache = Cache()
    // Empty the cache whenever the struct mutates.
    var value: Int {
        willSet {
            cache = Cache()
        }
    }
    // Prevent Swift from generating an unwanted default initializer.
    // (i.e. init(cache: Number.Cache, value: Int))
    init(value: Int) {
        self.value = value
    }
    var square: Int {
        // If the computed variable has been cached...
        if let result = cache.square {
            // ...return it.
            print("I’m glad I don’t have to do that again.")
            return result
        } else {
            // Otherwise perform the expensive calculation...
            print("This is taking forever!")
            var result = 0
            for var i = 1; i <= value; ++i {
                result += value
            }
            // ...store the result to the cache...
            cache.square = result
            // ...and return it.
                return result
        }
    }
    // A more complex example that caches the varying results
    // of performing an expensive operation on an input parameter.
    func multiple(coefficient: Int) -> Int {
        if let result = cache.multiples[coefficient] {
            return result
        } else {
            var result = 0
            for var i = 1; i <= coefficient; ++i {
                result += value
            }
            cache.multiples[coefficient] = result
                return result
        }
    }
}

这就是它的表现方式:

// The expensive calculation only happens once...
var number = Number(value: 1000)
let a = number.square // “This is taking forever!”
let b = number.square // “I’m glad I don’t have to do that again.”
let c = number.square // “I’m glad I don’t have to do that again.”
// Unless there has been a mutation since last time.
number.value = 10000
let d = number.square // “This is taking forever!”
let e = number.square // “I’m glad I don’t have to do that again.”
// The cache even persists across copies...
var anotherNumber = number
let f = anotherNumber.square // “I’m glad I don’t have to do that again.”
// ... until they mutate.
anotherNumber.value = 100
let g = anotherNumber.square // “This is taking forever!”

作为一个更现实的例子,我在日期结构上使用过这种技术,以确保尽可能少地运行用于在日历系统之间转换的非平凡计算。

这是一个非常有趣的问题。我这里有一些不同的想法可以提供帮助。

首先,您稍微滥用了lazy属性的概念。您只能具有惰性存储属性,因为惰性所做的只是延迟执行,直到首次执行为止。然后,从那时起,该值将stored在属性中。您正在处理不能以这种方式使用的计算属性。您当然可以提交雷达,但我认为这是一个失败的原因,因为您的用例不是有效的懒惰案例 IMO。

话虽如此,我认为您有几个选择。

选项 1 - 将类与属性观察器一起使用

class Calculator {
    var value: Int {
        didSet {
            valueChanged = true
        }
    }
    var valueChanged = false
    var derivedValue: Int {
        if valueChanged {
            println("Doing expensive calculation")
            valueChanged = false
        }
        return self.value * 2
    }
    init(value: Int) {
        self.value = value
    }
    func mutate() {
        value = random()
    }
}

这样做的好处是,您仍然可以在调用属性时延迟计算derivedValue。缺点是你不再使用"按值"对象。

选项 2 - 在突变方法中计算昂贵值

struct SortOfLazyCalculator {
    var value: Int
    var expensiveComputedValue: Int = 0 // just guessing
    var derivedValue: Int {
        return self.value * 2
    }
    init(value: Int) {
        self.value = value
    }
    mutating func mutate() {
        value = random()
        expensiveComputedValue = random() // not sure what the expensive calculation is
    }
}

这种方法的优点是,您仍然可以保留"按值"对象,但您必须在突变时计算昂贵的值。不能在 derivedValue 属性中执行此操作,因为不能在结构的计算属性中改变self

选项 3 - 使用静态结构监视值更改

struct Struct {
    var value: Int
    var derivedValue: Int {
        struct Static { static var previousValue: Int? }
        if Static.previousValue == nil {
            println("Setting previous value since it is nil")
            Static.previousValue = value
        }
        if value != Static.previousValue! {
            println("Doing expensive calculation")
            Static.previousValue = value
        }
        return self.value * 2
    }
    init(value: Int) {
        self.value = value
    }
    mutating func mutate() {
        value = random()
    }
}

此方法允许您保留"按值"对象,同时还允许您延迟计算昂贵的值。这里的主要问题是这仅适用于单个对象。如果要创建多个对象,则此方法不正确。

总结

不幸的是,这不是惰性属性的有效用例。但是,还有其他方法可以解决此问题。希望其中之一就足够了。根据您提供的所有信息,我冒昧地猜测选项 2 可能是您最好的选择。

最新更新