It's not an issue with State it looks a compiler issue.
struct S {
var a: Int?
var b: Optional<Int>
init() {
// error: Return from initializer without initializing all stored properties
}
}
a will automatically be initialized with nil as a convenience feature, but b will not!
Related topics:
- Pitch: Remove default initialization of optional bindings
- Pre-pitch: remove the implicit initialization of Optional variables
@Slava_Pestov @jrose maybe we should aim to remove this for Swift 6? The example of confusion about the effects it causes for State is a fair motivation. On top of that explicit Optional isn't treaded the same by the compiler anyways.
Edit:
I meant to reply in this thread SwiftUI @State PW exact same code different result if another optional is added!? :=( but accidentally replied here and just realized that @mayoff already explained the puzzle.