Devlog

hidden world

Posted at — Jan 21, 2024 DRAFT

this is erroring because of @Published.

class Test : ObservableObject {
    @Published var cubes: [String]
    @Published var planes: [String]

    init() {
        self.cubes = ["test_a"]
        let a = "test_a"
        self.cubes.append(a)

        self.planes = []
        let b = "test_b"
        self.planes.append(b)
    }
}

I asked to GPT and it doesn’t know exactly but I poked to answer anything then it’s like this.

The error message you're encountering, where self.planes is reported as being used before being initialized, might indeed be related to how memory is allocated and managed for properties marked with @Published in a class conforming to ObservableObject in SwiftUI.

Understanding what's happening under the hood requires diving into how SwiftUI and Combine handle property wrappers and observable objects. Here's a closer look:

Memory Allocation and Initialization in Swift
Two-Phase Initialization:

Swift's classes have a two-phase initialization process. In the first phase, each property of the class must be assigned an initial value. Only after all properties have been initialized can you call any instance methods, access any instance properties, or reference self.
@Published Property Wrapper:

The @Published property wrapper adds an additional layer to the property. It's not just a simple value storage; it also includes a publisher.
When a property is marked as @Published, it’s wrapped in a Published struct, which manages the storage and provides a Publisher.
This wrapping process might have implications for the initialization phase, especially in terms of when self is considered fully initialized.
Possible Reasons for the Error
@Published and Memory Management:

The compiler needs to ensure that all properties are fully initialized before self can be used. For @Published properties, this might involve setting up the publisher, which could internally reference self.
If the compiler detects that the memory for self might be accessed before all properties are initialized, it will throw an error.
SwiftUI and Combine Internal Mechanics:

SwiftUI and Combine might have internal mechanisms that reference self when setting up @Published properties. If these mechanisms are triggered before all properties are initialized, it could lead to the error.
Compiler Optimization and Safety Checks:

Swift’s compiler performs many optimizations and safety checks. It might be overly cautious in ensuring that all properties are initialized before any are used, especially when dealing with property wrappers that have complex behaviors, like @Published.

Resolution
In practice, the solution often involves ensuring that all @Published properties are initialized before they are used in any way, including any implicit use by the framework itself. This might mean changing the order of initialization or simplifying the initializer. However, if the error persists despite proper sequential initialization, it could be a bug or a limitation in the compiler's understanding of complex initializations involving property wrappers like @Published.

Conclusion
While the exact internal workings of the SwiftUI and Combine frameworks regarding property wrapper initialization are not fully documented, understanding the two-phase initialization process in Swift and the additional complexity introduced by property wrappers like @Published can help in diagnosing and resolving such issues. If these approaches don't resolve the issue, consider reaching out to the Swift developer community or filing a report with Apple, as it might be an edge case or bug in the compiler.

funny world. If you can ask better questions, it can be answer anything like it can ‘guess’. And.. when the things are getting convinient, more things are hidden under the hood. that’s sometimes frustrating because it seems like you can make something working ’easily than before’, but that doesn’t guarantee full quality of your work. there should be more than that.

comments powered by Disqus