[swift-evolution] [Discussion] Difference between static and lazy variables regarding evaluation of closure

Vladimir.S svabox at gmail.com
Fri Apr 8 11:50:27 CDT 2016


Yes, IMO it really looks strange.
Just checked:

     class Foo {
         static var test = "test"

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

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


     print("1")
     print(Foo.test)
     print("2")
     Foo.bar = "Set"
     print("3")
     let foo = Foo()
     foo.baz = "Set"
     print("4")

we have :
1
test
2
static
3
4

I strongly believe as static property is lazy by definition, it must not be 
evaluated at all when we set it. This is something that "lazyness" promises 
to us - that it will be called/calculated ONLY when we ask for this. So in 
my opinion this is bug/issue and should be fixed/changed in Swift 3.0.


On 08.04.2016 10:36, David Rönnqvist via swift-evolution wrote:
> I noticed a difference between how static and lazy variables evaluate closures and thought that it was a bug:
> https://bugs.swift.org/browse/SR-1178
> but apparently it’s not.
 > ...


More information about the swift-evolution mailing list