# \[Solved\] Compile error when putting init() in a separate file

**URL:** <https://forums.swift.org/t/solved-compile-error-when-putting-init-in-a-separate-file/63432>\
**Category:** Using Swift\
**Created:** [March 1, 2023, 2:56am UTC](https://forums.swift.org/t/solved-compile-error-when-putting-init-in-a-separate-file/63432 "2023-03-01T02:56:16Z")\
**Posts on this page:** 10\
**Page:** 1

<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:** [March 1, 2023, 2:56am UTC](https://forums.swift.org/t/solved-compile-error-when-putting-init-in-a-separate-file/63432/1 "2023-03-01T02:56:16Z")

</div>

See the code below. Putting it in the same file works fine, but putting it in two files causes compile error. I wonder why?

Based on the information [here](https://forums.swift.org/t/why-swiftui-state-property-can-be-initialized-inside-init-this-other-way/62772/2), the `x=1` line in `init()` should initialize the property wrapper. It seems that it fails to work for some reason in this case?

```swift
// File 1
@propertyWrapper
struct DummyWrapper<T>: Equatable where T: Equatable {
    var wrappedValue: T

    public init(wrappedValue: T) {
        self.wrappedValue = wrappedValue
    }
}

struct Foo {
    @DummyWrapper var x: Int
}

// File 2
extension Foo {
    init() {
        x = 1 // Error: 'self' used before all stored properties are initialized
    }
}

```

Background: I'd like to put `init()` in a separate file because I'm thinking to put all structs conforminng to `Codable` in the same folder. And put their methods (including `init()`) in other folders.

---

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [March 1, 2023, 2:59am UTC](https://forums.swift.org/t/solved-compile-error-when-putting-init-in-a-separate-file/63432/2 "2023-03-01T02:59:20Z")

</div>

Normally, I would say that `init` is special in that it should live in the type declaration itself and not an extension. However, that particular error is unexpected.

---

<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:** [March 1, 2023, 3:05am UTC](https://forums.swift.org/t/solved-compile-error-when-putting-init-in-a-separate-file/63432/3 "2023-03-01T03:05:24Z")

</div>

I see your point, though I often put `init()` in extensions for two reasons: a) just for organizing code, b) let the compiler synthesize the member-wise `init()`. I usually do them in the same file though.

BTW, the above error only occurs when the struct has property wrapper.

---

<div class="post-metadata">

**Author:** ![stackotter](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stackotter/32/20670_2.png) [@stackotter](https://forums.swift.org/u/stackotter)\
**Post date:** [March 1, 2023, 3:32am UTC](https://forums.swift.org/t/solved-compile-error-when-putting-init-in-a-separate-file/63432/4 "2023-03-01T03:32:41Z")

</div>

This occurs because `Foo` could potentially have `private` properties that you would not be able to access from your extension. Therefore, there are situations where it would be impossible for an initialiser in a separate file to fully initialise all properties. That's why Swift requires you to call an `init` that is defined in `File 1` before you access/modify any properties from within the `init` in `File 2`.

In this case you can make use of the default memberwise initialiser by replacing `x = 1` with `self.init(x: 1)` in the `init` you define in `File 2`. This will fix the error. You could then have a line after that such as `x = 2` and that would be perfectly valid because you've already initialised `Foo`.

---

<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:** [March 1, 2023, 3:47am UTC](https://forums.swift.org/t/solved-compile-error-when-putting-init-in-a-separate-file/63432/5 "2023-03-01T03:47:29Z")

</div>

> [@stackotter](#):
>
> This occurs because `Foo` could potentially have `private` properties that you would not be able to access from your extension. Therefore, there are situations where it would be impossible for an initialiser in a separate file to fully initialise all properties. That's why Swift requires you to call an `init` that is defined in `File 1` before you access/modify any properties from within the `init` in `File 2`.

Thanks. You're right. I made a change just now (see below). It also failed to compile but generated a different error, which indicated `_x` has private access.

```swift
- x = 1
+ _x = DummyWrapper(wrappedValue: 1)

// Error: '_x' is inaccessible due to 'private' protection level

```

> [@stackotter](#):
>
> In this case you can make use of the default memberwise initialiser by replacing `x = 1` with `self.init(x: 1)` in the `init` you define in `File 2`.

Yes, it works. Unfortunately it made the code a bit hard to follow, which defeats the original purpose. I'll consider to move the `init()` to the same file. Thanks.

---

<div class="post-metadata">

**Author:** ![stackotter](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stackotter/32/20670_2.png) [@stackotter](https://forums.swift.org/u/stackotter)\
**Post date:** [March 1, 2023, 3:52am UTC](https://forums.swift.org/t/solved-compile-error-when-putting-init-in-a-separate-file/63432/6 "2023-03-01T03:52:25Z")

</div>

You're welcome, glad I could help

> [@rayx](#):
>
> I'll consider to move the `init()` to the same file.

Yeah you should generally put the init in the same file as the type declaration unless you have a really good reason not to (in my opinion). An example of when I might have the init in a separate file is when I'm modularising my code and I want to for example add conformance to `Codable` for multiple types in one spot to separate caching functionality from the types themselves. Hope that guidance helps

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [March 1, 2023, 4:04am UTC](https://forums.swift.org/t/solved-compile-error-when-putting-init-in-a-separate-file/63432/7 "2023-03-01T04:04:57Z")

</div>

> [@stackotter](#):
>
> This occurs because `Foo` could potentially have `private` properties

The underscored instance property for wrappers is always private.

> [@rayx](#):
>
> `_x` has private access.

That's the one!

As such, this is the general workaround form:

```swift
// File 1
extension Foo {
  init(x: DummyWrapper<Int>) {
    _x = x
  }
}

// File 2
extension Foo {
  init() {
    self.init(x: .init(wrappedValue: 1))
  }
}

```

---

<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:** [March 1, 2023, 4:16am UTC](https://forums.swift.org/t/solved-compile-error-when-putting-init-in-a-separate-file/63432/8 "2023-03-01T04:16:37Z")

</div>

@anon9791410 it can be simpler. Below is what @stackotter suggested:

```swift
// File 1
// Don't define init() explicitly. Just use the default member-wise init().

// File 2
extension Foo {
    init() {
        self.init(x: 1)
    }
}

```

It's simpler because it takes advantage of the small feature mentioned [here](https://forums.swift.org/t/why-swiftui-state-property-can-be-initialized-inside-init-this-other-way/62772/2).

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [March 1, 2023, 4:17am UTC](https://forums.swift.org/t/solved-compile-error-when-putting-init-in-a-separate-file/63432/9 "2023-03-01T04:17:59Z")

</div>

That's not the same thing. That's a subset of the general form—for when you don't need to pass in wrappers.

---

<div class="post-metadata">

**Author:** ![stackotter](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stackotter/32/20670_2.png) [@stackotter](https://forums.swift.org/u/stackotter)\
**Post date:** [March 1, 2023, 8:12am UTC](https://forums.swift.org/t/solved-compile-error-when-putting-init-in-a-separate-file/63432/10 "2023-03-01T08:12:18Z")

</div>

> [@anon9791410](#):
>
> That's not the same thing. That's a subset of the general form—for when you don't need to pass in wrappers.

The reason that I specified the `self.init(x: x)` form is because I was answering the question in general for any code that gets that error `'self' used before all stored properties are initialized`. I believe that that error would happen even if `Foo` didn't have any private properties. And it's also not specific to property wrappers.
