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

[SR-1178] Closure for initializing static variables is evaluated even when the variable is set with a different value #43786

Open
d-ronnqvist opened this issue Apr 7, 2016 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. feature A feature request or implementation

Comments

@d-ronnqvist
Copy link
Contributor

Previous ID SR-1178
Radar None
Original Reporter @d-ronnqvist
Type Bug
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug, LanguageFeatureRequest
Assignee None
Priority Medium

md5: 1a0479e8c26ec52f84890efb1cc77bec

Issue Description:

When defining and evaluating a closure to initialize a variable, i.e.

var name: String = { /* some initialization */ }()

to initialize a static variable, the closure is evaluated even if the variable is only set.

As a minimal example, this code will print "static"

class Foo {
    static var bar: String = {
        print("static")
        return "Default"
    }()
}

Foo.bar = "Set"

This differs from the behavior of lazy instance variables. The following code (note lazy instead of static) does not evaluate the closure and doesn't print anything.

class Foo {
    lazy var baz: String = {
        print("lazy")
        return "Lazy"
    }()
}

let foo = Foo()
foo.baz = "Set"
@belkadan
Copy link
Contributor

belkadan commented Apr 7, 2016

This is currently by design, but it might be worth changing the design.

@d-ronnqvist
Copy link
Contributor Author

I see. How should I proceed with this? A discussion on the mailing-list?

@belkadan
Copy link
Contributor

belkadan commented Apr 7, 2016

I suppose so. I expect many people to have opinions here. The discrepancy between lazy and static definitely seems like a problem to me; we should pick one rule and stick with it.

@swift-ci
Copy link
Collaborator

Comment by Basem Emara (JIRA)

This `static` setter calling the getter forces several awkward workarounds, especially with dependency injection. The `static` setter should not also call the original getter. It is very unintuitive and unsafe. Please consider fixing this so `Foo.bar = "Set"` doesn't also call the getter. The `lazy` implementation is correct.

See this StackOverflow discussion about this: http://stackoverflow.com/questions/43374222/in-swift-why-does-assigning-to-a-static-variable-also-invoke-its-getter

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. feature A feature request or implementation
Projects
None yet
Development

No branches or pull requests

3 participants