Ya this largely mitigates my concern about SwiftUI and initialized variables, though I still worry if we came up with a compelling reason for Void results in the future, we would be forced to allow these assignments.
My meta-concern is that we're lifting these restrictions without concrete, compelling motivations.
However, I came up with a use case at least for lifting the restriction of using property wrappers in result builders that I find compelling. In particular, the @Binding property wrapper is primarily providing a syntactic illusion, and the motivation for using it in local variables within result builders is consistent with the motivation in SE-0293.
Here's a concrete example that could take advantage of this proposal:
struct StudyNowView: View {
struct ViewState {
private var _isStudying: Bool = false
subscript(reviewCount n: Int) -> Bool {
get { _isStudying && n > 0 }
set { _isStudying = newValue }
}
}
@State private var state = ViewState()
var session: StudySession
var body: some View {
@Binding var isStudying = $state[reviewCount: session.reviewCount]
OtherIrrelevantContent()
.sheet(isPresented: $isStudying) {
StudySessionView(session: session, onDismiss: { isStudying = false })
}
}
}
Sorry for the involved example, but it's from a personal app I'm working on. The StudySessionView is modally presented as long as the user hasn't explicitly dismissed it and there are still flash cards left to review. The projection via subscript is combining the model data with the local state data.
Being able to use the @Binding local variable allows me to avoid (1) repeating myself:
.sheet(isPresented: $state[reviewCount: session.reviewCount]) {
StudySessionView(session: session, onDismiss: { state[reviewCount: session.reviewCount] = false })
}
and (2) breaking the syntactic illusion:
let isPresented = $state[reviewCount: session.reviewCount]
OtherIrrelevantContent()
.sheet(isPresented: isPresented) { // no $, so unclear if read-only or read-write
StudySessionView(session: session, onDismiss: { isPresented.wrappedValue = false })
}
So you can convert me to a +1 for using lifting the restriction of using property wrappers in result builders.