[Pitch] if and switch expressions

Nice work, Ben and Hamish.

Is there any chance of getting this much-upvoted suggestion into the proposal — if not as part of what’s actually proposed for this round, then at least as a future direction?

Lack of full generality was one of the most common objections above, and doubtless will be so in formal review as well. People seem to like the solution above. It addresses all of the concerns in the Future Directions | Full Expressions section of the proposal:

// ❌ wat
for b in [true] where switch b { case true: true case false: false } {}

// ✅ meaningless, but easy enough for humans to parse visually
for b in [true] where (switch b { case true: true case false: false }) {}
var body: some View {
    VStack {
        if showButton {
            Button("Click me!", action: clicked)
        } else {
            Text("No button")
        }
        .someProperty  // ❌ parsing ambiguity: static property, or property of View?
    }
}

var body: some View {
    VStack {
        (
            if showButton {
                Button("Click me!", action: clicked)
            } else {
                Text("No button")
            }
        ).someProperty // ✅ no parsing ambiguity
    }
}

…and I don’t think anybody identified a downside.

I don’t want the idea to get lost. It may well be too much to consider in this proposal, but at a minimum, it’s a useful answer to those future direction concerns.

9 Likes