Demonstrate Swift's Lazy Initialization of Static Attributes Even when Using a Property Wrapper

I wanted to make sure that I’m up-to-date in regards to Swift’s lazy initialization of static attributes.

So here’s a demonstration of a Swift property wrapper that produces debug output to check what’s going on, aka caveman debugging (because of the log statement, y'know).

The output is as follows, demonstrating that the static var is initialized lazily when you access it:

create object
init 123
get ivar
get 123
get classvar
init 567
get 567

The code:

@propertyWrapper
public struct Setting<ValueType> {
    public let value: ValueType

    public init(value: ValueType) {
        print("init", value)
        self.value = value
    }

    public var wrappedValue: ValueType {
        get {
            print("get", value)
            return value
        }
        set {
            print("set", value)
        }
    }
}

class Foo {
    @Setting(value: 123) var ivar: Int
    @Setting(value: 567) static var classvar: Int
}

print("create object")
let f = Foo()
print("get ivar")
_ = f.ivar
print("get classvar")
_ = Foo.classvar

Receive new .