# Can I rely on the order of property initialization?

**URL:** <https://forums.swift.org/t/can-i-rely-on-the-order-of-property-initialization/50449>\
**Category:** Using Swift\
**Tags:** abistability, swift\
**Created:** [July 14, 2021, 4:44pm UTC](https://forums.swift.org/t/can-i-rely-on-the-order-of-property-initialization/50449 "2021-07-14T16:44:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![theMomax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/themomax/32/22719_2.png) [@theMomax](https://forums.swift.org/u/theMomax)\
**Post date:** [July 14, 2021, 4:44pm UTC](https://forums.swift.org/t/can-i-rely-on-the-order-of-property-initialization/50449/1 "2021-07-14T16:44:20Z")

</div>

Can I trust on properties with an initial value to always be initialized in the same order?

E.g. take the following code:

```swift
import Foundation

@propertyWrapper
struct Printed<T> {
    var wrappedValue: T
    
    init(wrappedValue: T) {
        print(wrappedValue)
        self.wrappedValue = wrappedValue
    }
}

struct MyStruct {
    @Printed var one = 1
    @Printed var two = 2
    @Printed var three = 3
    @Printed var four = 4
    @Printed var five = 5

    init(override: Int) {
	_five = Printed(wrappedValue: override)	
    }
}

_ = MyStruct(override: 100)

```

For me, this always prints 1, 2, 3, 4, 5, 100. Can I rely on that, or could the compiler (on some platform, with some optimization, or even in a certain context in which the `MyStruct` initializer is called) decide to mix up the order of initialization for the initial values 1 to 5? Is something like that considered part of the language's interface and thus covered by stability guarantees, or could this change with every version?

In case the compiler is allowed to change that order, under what circumstances does it do so?

---

<div class="post-metadata">

**Author:** ![Andrew\_Trick](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew_trick/32/323_2.png) [@Andrew\_Trick](https://forums.swift.org/u/Andrew_Trick)\
**Post date:** [July 14, 2021, 5:00pm UTC](https://forums.swift.org/t/can-i-rely-on-the-order-of-property-initialization/50449/2 "2021-07-14T17:00:07Z")

</div>

Never fear, side effects like `print` in initializers are ordered. It's only side effects in different `deinit`s that are unordered.

---

<div class="post-metadata">

**Author:** ![theMomax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/themomax/32/22719_2.png) [@theMomax](https://forums.swift.org/u/theMomax)\
**Post date:** [July 15, 2021, 6:02am UTC](https://forums.swift.org/t/can-i-rely-on-the-order-of-property-initialization/50449/3 "2021-07-15T06:02:33Z")

</div>

That's great news! Thanks a lot for the quick response👍🏻

---

<div class="post-metadata">

**Author:** ![rayx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rayx/32/12204_2.png) [@rayx](https://forums.swift.org/u/rayx)\
**Post date:** [July 15, 2021, 2:20pm UTC](https://forums.swift.org/t/can-i-rely-on-the-order-of-property-initialization/50449/4 "2021-07-15T14:20:16Z")

</div>

I think Andrew didn't give a full answer to Max's question?

I think `print` in the same thread are ordered (thought it may be buffered) in all the OS that I know about. For the `deinit` case, I suppose it's because `deinit` is unordered and so are the `print` call in different `deinit`.

But what Max asked was not this. It's about property initialization order. I doubt if that's part of the language spec. Perhaps it's more safe to implement your own mechanism to achieve that behavior outside initialization process? Just my thoughts.

---

<div class="post-metadata">

**Author:** ![theMomax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/themomax/32/22719_2.png) [@theMomax](https://forums.swift.org/u/theMomax)\
**Post date:** [July 15, 2021, 2:26pm UTC](https://forums.swift.org/t/can-i-rely-on-the-order-of-property-initialization/50449/5 "2021-07-15T14:26:42Z")

</div>

For my use-case the side effect would be appending a value to a static Array variable (on the same thread), so I guess that would be covered by @Andrew_Trick 's answer?

---

<div class="post-metadata">

**Author:** ![william5848](https://avatars.discourse-cdn.com/v4/letter/w/6de8d8/32.png) [@william5848](https://forums.swift.org/u/william5848)\
**Post date:** [July 16, 2021, 5:08am UTC](https://forums.swift.org/t/can-i-rely-on-the-order-of-property-initialization/50449/6 "2021-07-16T05:08:11Z")

</div>

Please to see your question, I think i have same question but now it will be solved.  
Thanks to all community member for sharing your helpful suggestion's.

---

<div class="post-metadata">

**Author:** ![Andrew\_Trick](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew_trick/32/323_2.png) [@Andrew\_Trick](https://forums.swift.org/u/Andrew_Trick)\
**Post date:** [July 16, 2021, 6:13am UTC](https://forums.swift.org/t/can-i-rely-on-the-order-of-property-initialization/50449/8 "2021-07-16T06:13:32Z")

</div>

Writing to a static array (global variable) would be reliably ordered just like a print statement.

I can tell you that compiler optimization will not reorder these side effects. I cannot tell you whether initialization order is formally guaranteed by the language, but I personally don't think it would be reasonable for any implementation to break that expectation.

---

<div class="post-metadata">

**Author:** ![theMomax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/themomax/32/22719_2.png) [@theMomax](https://forums.swift.org/u/theMomax)\
**Post date:** [July 16, 2021, 8:00am UTC](https://forums.swift.org/t/can-i-rely-on-the-order-of-property-initialization/50449/9 "2021-07-16T08:00:53Z")

</div>

Ok, I guessed so, but before I would invest time into it I wanted to make sure its not all wasted. Thanks @Andrew_Trick that answer is totally fine for me!
