# Writing to a static property causes its default value to be evaluated - is this deliberate? A bug? Could it be changed?

**URL:** <https://forums.swift.org/t/writing-to-a-static-property-causes-its-default-value-to-be-evaluated-is-this-deliberate-a-bug-could-it-be-changed/60235>\
**Category:** Discussion\
**Tags:** lazy, optimizations, static\
**Created:** [September 11, 2022, 11:24pm UTC](https://forums.swift.org/t/writing-to-a-static-property-causes-its-default-value-to-be-evaluated-is-this-deliberate-a-bug-could-it-be-changed/60235 "2022-09-11T23:24:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [September 11, 2022, 11:24pm UTC](https://forums.swift.org/t/writing-to-a-static-property-causes-its-default-value-to-be-evaluated-is-this-deliberate-a-bug-could-it-be-changed/60235/1 "2022-09-11T23:24:38Z")

</div>

```swift
enum Foo {
    static var bar: Int = { print("bar default value evaluated"); return 0 }()
    static var baz: Bool = bar % 2 == 0
}
func demo () {
    print("a")
    Foo.bar = 7
    print("b")
    print(baz)
}
/*
prints:
a
bar default value evaluated
b
false
*/

```

I would have assumed that the default value would not be evaluated if the first thing I do with the static property is to set a new value, and that therefore the console would read:

```swift
a
b
false

```

Can anyone tell me more about this? I think I would prefer that it behaved in the way I was expecting, although I suppose it doesn't matter all that much.

---

<div class="post-metadata">

**Author:** ![patrickgoley](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/patrickgoley/32/19398_2.png) [@patrickgoley](https://forums.swift.org/u/patrickgoley)\
**Post date:** [September 11, 2022, 11:46pm UTC](https://forums.swift.org/t/writing-to-a-static-property-causes-its-default-value-to-be-evaluated-is-this-deliberate-a-bug-could-it-be-changed/60235/2 "2022-09-11T23:46:02Z")

</div>

I imagine this is because `bar` _could_ have a `didSet` implemented, which receives the `oldValue` which would need to be initialized to something. So I don't think it's a bug, but perhaps something that could be optimized away in the case where a `didSet` does not exist.

In general I think Swift errs on the side of making sure things are initialized, especially in the case of a static var where the initialization will occur exactly once so the wasted effort / performance hit won't add up over time.

Edit:  
I think the current behavior also avoids a subtle side effect that would appear if such an optimization was introduced. It might be surprising to some that by adding a `didSet` to `foo` would cause the default initialization code to run when it previously did not. Sort of a spooky action at a distance that could subtly alter the semantics of your program, especially if the initializer has side effects.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [September 12, 2022, 12:36am UTC](https://forums.swift.org/t/writing-to-a-static-property-causes-its-default-value-to-be-evaluated-is-this-deliberate-a-bug-could-it-be-changed/60235/3 "2022-09-12T00:36:46Z")

</div>

See [[SR-1178] Closure for initializing static variables is evaluated even when the variable is set with a different value · Issue #43786 · apple/swift · GitHub](https://github.com/apple/swift/issues/43786) and @hamishknight’s answer here: [In Swift, why does assigning to a static variable also invoke its getter - Stack Overflow](https://stackoverflow.com/a/43379040)

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [September 12, 2022, 12:37am UTC](https://forums.swift.org/t/writing-to-a-static-property-causes-its-default-value-to-be-evaluated-is-this-deliberate-a-bug-could-it-be-changed/60235/4 "2022-09-12T00:37:50Z")

</div>

> [@patrickgoley](#):
>
> I imagine this is because `bar` _could_ have a `didSet` implemented, which receives the `oldValue` which would need to be initialized to something.

Note that we no longer fetch the `oldValue` if the body of the `didSet` does not have a reference to it or if it’s not explicitly requested in the parameter list aka `didSet(oldValue)` (SE-0268).

---

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [September 12, 2022, 1:42am UTC](https://forums.swift.org/t/writing-to-a-static-property-causes-its-default-value-to-be-evaluated-is-this-deliberate-a-bug-could-it-be-changed/60235/5 "2022-09-12T01:42:59Z")

</div>

> [@suyashsrijan](#):
>
> Note that we no longer fetch the `oldValue` if the body of the `didSet` does not have a reference to it or if it’s not explicitly requested in the parameter list aka `didSet(oldValue)` (SE-0268).

I think this is very interesting with regard to this possible concern:

> [@patrickgoley](#):
>
> It might be surprising to some that by adding a `didSet` to `foo` would cause the default initialization code to run when it previously did not.

because now I think that there is a very coherent, non-spooky new behavior that we could change to:

If the first time that you access a static variable is to set its value then in most cases the default value provided at the property definition will not be evaluated (which can be seen as logical and in-line with, for example, the fact that a default function argument is not evaluated if the caller supplies a value). The only condition under which setting the static value causes the default value to be evaluated is not merely the presence of a `didSet`, but more specifically the presence of a `didSet` that explicitly requests access to the `oldValue`.

To me it seems like explicitly requesting a guaranteed (non-optional) `oldValue` in the `didSet` is equivalent to requesting that the initial value be brought into play, which makes the fact that it now is evaluated whereas it previously wasn't more obvious than spooky.

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [September 12, 2022, 3:33pm UTC](https://forums.swift.org/t/writing-to-a-static-property-causes-its-default-value-to-be-evaluated-is-this-deliberate-a-bug-could-it-be-changed/60235/6 "2022-09-12T15:33:46Z")

</div>

I kind of want to go in a completely opposite direction with this. Should static properties really be implicitly lazy?

1. With constant expressions, there’s no such thing as lazy initialization. Should Swift have two different flavors of static property differentiated by the capabilities of dataflow analysis?
2. Initializer expressions can have side effects. It’s not obvious—especially for programmers familiar with other languages that have a `static` keyword—that every static property implies two states: one in which the side effects have been run, and one in which they haven’t. The proposal of skipping the initializer expression entirely when assigning threatens to persist that first hidden state.

---

<div class="post-metadata">

**Author:** ![Nobody1707](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nobody1707/32/2169_2.png) [@Nobody1707](https://forums.swift.org/u/Nobody1707)\
**Post date:** [September 12, 2022, 4:14pm UTC](https://forums.swift.org/t/writing-to-a-static-property-causes-its-default-value-to-be-evaluated-is-this-deliberate-a-bug-could-it-be-changed/60235/7 "2022-09-12T16:14:43Z")

</div>

Static properties are lazy to avoid C++'s [static initialization order fiasco](https://en.cppreference.com/w/cpp/language/siof). Proper compile time constants wouldn't suffer that problem anyway, which is why C++20 added `constinit`. I think it's perfectly reasonable for compile time (or compile time initialized) Swift static variables to be initialized differently than regular ones. Not that we're likely to get them any time soon.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [September 12, 2022, 4:25pm UTC](https://forums.swift.org/t/writing-to-a-static-property-causes-its-default-value-to-be-evaluated-is-this-deliberate-a-bug-could-it-be-changed/60235/8 "2022-09-12T16:25:28Z")

</div>

Global/static variables with constant expression initializers can and do get optimized into constant initializers already. It would be reasonable to have an annotation that requires the initializer to be constant-foldable. For side-effectful initializers, initialization on first use is less bad than any of the alternatives (as @Nobody1707 noted). C++11 also uses initialization on first use for local `static` variables.

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [September 12, 2022, 7:04pm UTC](https://forums.swift.org/t/writing-to-a-static-property-causes-its-default-value-to-be-evaluated-is-this-deliberate-a-bug-could-it-be-changed/60235/9 "2022-09-12T19:04:51Z")

</div>

> [@Joe\_Groff](#):
>
> For side-effectful initializers, initialization on first use is less bad than any of the alternatives (as @Nobody1707 noted).

I would argue that unconditional initialization on first access, whether read or write, is similarly less bad than the alternative of conditional initialization based on whether the first access is a read or a write. Otherwise the program has two possible branches of state that can change based on adding, removing, or reordering a read from an otherwise-idempotent getter.
