Hi all,
if I declare a property as "static var" it apparently automatically gets initialized lazily, i.e., only at the time it is first used. Is there are way to prevent this?
class Features
{
static var feature1 = Feature()
static var feature2 = Feature()
..
}-> as soon as the class Features is used, all init() of Feature should be called
Thanks,
Markus
From the Swift blog:
developer.apple.com/swift/blog/?id=7
"Swift uses the third approach [Initialize lazily, run the initializer for a global the first time it is referenced]. […] The lazy initializer for a global variable (also for static members of structs and enums) is run the first time that global is accessed"
You have to ask yourself why you think that the compile-time declaration should tell the compiler anything about the run-time timing of initialization. Also, when — in run-time terms — does "as soon as the class Features is used" happen? App launch? Class framework load? First instance init? Also, if it matter when Feature() is called, what order should feature1 and feature2 be initialized? What if they're in different class extensions? The answers to such questions aren't obvious, except in an artifically simple example.