Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Lazy property wrapper is loaded eagerly #20

Open
gshahbazian opened this issue Mar 6, 2020 · 4 comments · Fixed by #25
Open

@Lazy property wrapper is loaded eagerly #20

gshahbazian opened this issue Mar 6, 2020 · 4 comments · Fixed by #25
Labels
bug Something isn't working

Comments

@gshahbazian
Copy link

gshahbazian commented Mar 6, 2020

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)
    ]
}
@KyNorthstar KyNorthstar added the bug Something isn't working label Jul 27, 2020
@KyNorthstar
Copy link
Collaborator

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

@KyNorthstar KyNorthstar pinned this issue Jul 29, 2020
@KyNorthstar
Copy link
Collaborator

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
Copy link
Collaborator

KyNorthstar commented Aug 2, 2020

@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
Copy link
Collaborator

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

@KyNorthstar KyNorthstar reopened this Jan 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants