Swift-Lazy-Containers icon indicating copy to clipboard operation
Swift-Lazy-Containers copied to clipboard

@Lazy property wrapper is loaded eagerly

Open gshahbazian opened this issue 5 years ago • 5 comments

This is a test that ensures the init of a class isn't run when being stored at an @Lazy property. The test fails (and matches what I saw in real world usage), which looks like the property is loaded eagerly.

import XCTest
@testable import LazyContainers

var shouldNotRun = false

class ShouldNotInit {
    init() {
        shouldNotRun = true
    }
}

final class LazyContainersTests: XCTestCase {

    @Lazy
    var lazyShouldNotRun = ShouldNotInit()
    
    func testLazyInitWithPropertyWrapper() {
        XCTAssertFalse(shouldNotRun)
    }

    static var allTests = [
        ("testLazyInitWithPropertyWrapper", testLazyInitWithPropertyWrapper)
    ]
}

gshahbazian avatar Mar 06 '20 03:03 gshahbazian

Oh wow that's definitely a bug! I'll see what I can do about this

KyNorthstar avatar Jul 27 '20 20:07 KyNorthstar

Well this is concerning... I have reproduced your issue in Xcode 11.6, but not in Xcode 12 (Beta 3)... So maybe something changed between Swift 5.2.4 and 5.3? Gonna be hard to pin down a solution.

KyNorthstar avatar Jul 30 '20 03:07 KyNorthstar

@gshahbazian thanks for reporting this - I've released a new version with a bodgey workaround, to be removed once Swift 5.2 support is dropped

KyNorthstar avatar Aug 02 '20 05:08 KyNorthstar

Just realized I forgot to mark the property wrapper initializer for ResettableLazy. Whoops!

KyNorthstar avatar Jan 22 '21 21:01 KyNorthstar

Version 6 of this package will remove support for Swift ≤5, which means that the package won't compile where this issue was present.

That means version 6 effectively "fixes" this issue by requiring a Swift version where this issue isn't present

KyNorthstar avatar Dec 26 '24 23:12 KyNorthstar