SE-0373: Lift all limitations on variables in result builders

Okay. Note that the ability to do assignments is not new in this proposal, only the ability to do assignments that act as initializations. That is, you can currently write this:

  var x = 0
  x = 5

The assignment will produce a Void result that will be fed to the result builder. So if SwiftUI ever wants to support Void results without supporting assignments, it will already need a change. (Probably the right way to impose such a language restriction would be to make buildExpression a macro.)

7 Likes

Although it's quite easy to make SwiftUI accept Void results:

import SwiftUI

extension ViewBuilder {
    public static func buildExpression(_ expression: Void) -> EmptyView {
        EmptyView()
    }

    public static func buildExpression<V: View>(_ view: V) -> V {
        view
    }
}
2 Likes

+1

In many cases builders are only used to collect values of a certain type that are not bound to variables ("ArrayBuilder"). For these kinds of builders I don't see a reason to put any restrictions on syntactic constructs. Ideally one would be able to run arbitrary Swift code and get a list of the values that were collected during execution.

I would also be in favor of allowing control statements like while, break and continue if buildArray is implemented.

1 Like

SE-0373 has been accepted; thank you, all, for your part in this review.

John McCall
Review Manager

2 Likes