使用lazy属性的Swift协议 - 无法在不可变值上使用变异getter:'$0'是不可变的。

3

目标:创建一个协议,允许对符合协议的结构体进行属性的惰性计算,并且将这些结构体的属性添加到一个数组中。这种计算是密集的,应该只执行一次,因此需要使用lazy关键字。

所以,经过大量阅读(例如:Swift Struct with Lazy, private property conforming to Protocol)和试错(除非它确实涉及到这个完全相同的情况,否则请不要将此标记为重复),我想出了以下可行的方法:

import Foundation

protocol Foo {
    var footype: Double { mutating get }

    func calculateFoo() -> Double
}

struct Bar: Foo {
    private lazy var _footype: Double = {
        let value = calculateFoo()

        return value
    }()

    var footype: Double {
        mutating get {
            return _footype
        }
    }

    func calculateFoo() -> Double {
        print("calc")
        return 3.453
    }
}

在游乐场中测试:

var bar = Bar()
print(bar.footype)
print(bar.footype)

输出结果为:

calc
3.453
3.453

目前为止,一切都很顺利。

现在,我想创建一个Bar数组并添加footype属性:

var bar1 = Bar()
var bar2 = Bar()
var bar3 = Bar()
var bars = [bar1, bar2, bar3]

print(bars.map { $0.footype }.reduce(0.0, +))

我遇到了以下错误:

Cannot use mutating getter on immutable value: '$0' is immutable

找到了很多关于此错误的信息,但是我不知道该如何解决。一种方法是使用 class 而不是 struct,但这与代码的其他部分不兼容。

我尝试的东西是否可行?

1个回答

4

根据错误信息,您可以看出 $0 是不可变的,所以您不能在其上使用可变成员。

因此,您不能直接迭代 bars。您可以通过其 indices 进行迭代,因为我们知道 bars[$0] 是可变的:

print(bars.indices.map { bars[$0].footype }.reduce(0.0, +))

对于我的lazy属性实现,您有什么评论吗? - koen
1
@Koen,你似乎只是在使用链接答案中的解决方案,这很好。但是,我不会将calculateFoo放在协议中,因为它听起来像是实现细节。你可以在代码审查中发布此内容以获得更多反馈。 - Sweeper
在这里发布:https://codereview.stackexchange.com/questions/230651/swift-protocol-with-lazy-property-requirement - koen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接