[Pitch] Last expression as return value

It's worth repeating that Result Builders still pose a problem for this pitch, and I haven't yet seen a good answer to this concern. Today, we already have at least 3 very different paradigms that are semantically expressed with the same language-level control-flow primitives if and switch:

  1. plain-old control flow
  2. Result Builders. This looks like control flow, but it's definitely not.
  3. if and switch expressions. This also looks like control flow, but it's not (today). But after this pitch, it will also be allowed to include control flow and other statements?

At the call-site these three paradigms are often indistinguishable. Here's one of the first things you learn in SwiftUI:

struct MyView: View {
  @State private var showingText = false
  var body: some View {
    if showingText {
      Text("Now you can see the text")
    } 
    Button(if showingText { "Hide" } else { "Show" }) {
      showingText.toggle()
    }
  }
}

Which paradigm is the if using now? 2? 3?

If it's an if expression, then that's not valid because all branches have to be the same type and Text and Button are not the same type.

If it's a Result Builder (like it is today), then all the branches do not have to be the same type. So it would be valid. But why wouldn't it be using paradigm 3? If a junior dev, or a new Swift learner mistakenly thinks that it's using paradigm 3, when it's actually using 2, then how do we explain to them how to know which paradigm applies and when?

6 Likes