I am delighted to see this much-needed proposal seeing the light of day at last! It is indeed a conspicuous gap.
I have two suggestions, which I’ll post in separate messages to avoid reply confusion. First suggestion:
Consider allowing conditionals in arbitrary expression positions when enclosed in parentheses.
The lack of full generality of the proposal as it stands is bothersome to me.
What if we took this proposal as is for the 3 allowed cases — returns, assignments, and declaration initializers — but then allowed if / switch expressions in arbitrary other positions if and only if enclosed in parentheses? Thus:
var x = 1 + if foo { y } else { z } // not allowed
var x = 1 + (if foo { y } else { z }) // allowed
Thus the potentially ambiguous example in the proposal’s Future Directions section retains its original, current meaning:
VStack {
if showButton {
Button("Click me!", action: clicked)
} else {
Text("No button")
}
.someProperty
}
…because parentheses (currently illegal, thus no breakage!) would be necessary to get someProperty
of the Button
/ Text
values from the conditional:
VStack {
(if showButton {
Button("Click me!", action: clicked)
} else {
Text("No button")
})
.someProperty // One would probably format this on the same
// line as `)`; just making the point that
// the ambiguity the proposal mentions is resolved.
}