@State of optional: type spelling Optional<Int> is not the same as Int?

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:

@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: :man_facepalming: 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.

1 Like