Array with lazy elements

2019-06-05 08:21发布

问题:

I was wondering if there is a construct in the Swift 3 programming language that allows me to store some objects in an array, but initialize each element in this array lazily.

Imagine this example class:

class A {
    let test = "hello"
    let test2 = 2.0
}

Now I want to store an array of 'A' objects in an array of another class, like this:

class B {
    var lazy(?) array: [A] = {
        // Some code to initialize the element being accessed
    }()
}

If I access any element now, it would be cool if it is initialized just when I am accessing it, so lazily

print(B.array[1].test) (element at index one is now initialized)

Is this possible?

回答1:

You could use a lazy backing storage for A that is instantiated at first access, e.g.:

class Astorage {
    /* ... lots of ... */
    let id: Int
    var foo: String = "bar"
    init(_ id: Int) {
        self.id = id
        print("Initializing backing storage for A with id \(id)")
        // ...
    }
}

class A {
    private let id: Int
    lazy var storage: Astorage = Astorage(self.id)
    init(_ id: Int) {
        self.id = id
    }
}

class B {
    var array: [A]
    init (_ array: [A]) {
        self.array = array
    }
}

let b = B((1...5).map(A.init))

b.array[2].storage.foo = "foo"
// Initializing backing storage for A with id 3

b.array[4].storage.foo = "bax"
// Initializing backing storage for A with id 5


回答2:

You can use an enum and a mutating struct for this

enum LazyValueEnum<T>: CustomStringConvertible {
    case value(T)
    case thunk((Void) -> T)

    var forceValue: T {
        switch self {
        case .value(let t):
                return t
        case .thunk(let thunk):
                return thunk()
        }
    }

    var description: String {
        switch self {
        case .value(let t):
            return String(describing: t)
        case .thunk(_):
            return "<thunk>"
        }
    }
}

struct LazyValue<T>: CustomStringConvertible {

    var value: LazyValueEnum<T>

    init(_ t: T) {
        self.value = .value(t)
    }

    init(lazy thunk: @escaping (Void) -> T) {
        self.value = .thunk(thunk)
    }

    static func lazy(_ thunk: @escaping (Void) -> T) -> LazyValue<T> {
        return LazyValue(lazy: thunk)
    }

    static func value(_ value: T) -> LazyValue<T> {
        return LazyValue(value)
    }

    mutating func forceValue() -> T {
        let t = value.forceValue
        value = .value(t)
        return t
    }

    var description: String {
        return value.description
    }
}


var a: [LazyValue<Int>] = [.value(5), .value(7), .lazy { Int(arc4random()) }]
print(a)
print(a[2].forceValue())
print(a)
print(a[2].forceValue())
print(a)

Results from a playground: