Remove all limitations on variables in result builders

⋅ 1 min read ⋅ Swift Swift 5.8

Table of Contents

Before Swift 5.8, declaring local variables in result builders had many constraints.

Local variables need to:

  • Have an initializer expression
  • Cannot be computed
  • Cannot have observers
  • Cannot have attached property wrappers
  • And other constraints

These constraints contradict the result builder proposal, which states that local declarations are unaffected by the transformation.

Declaration statements
Local declarations are left alone by the transformation. This allows developers to factor out subexpressions freely to clarify their code, without affecting the result builder transformation.

Before Swift 5.8, you couldn't declare variables in any of these forms.

struct FooViewBuilder: View {
var body: some View {
var computedVar: String {
return "Computed"
lazy var lazyVar = compute()
let uninitializedVar: String
let defaultNilVar: String?
@AppStorage("example_string") var propertyWrapperVar = "Foo"

VStack {

func compute() -> String {
return "Foo"

Swift 5.8 (SE-0373) removes all of these limitations. So, the above code would build without any errors in Swift 5.8.

Read more article about Swift, Swift 5.8, or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Detecting if SwiftUI app is in Xcode Previews

Learn how to detect whether an app is running in Xcode Previews.

Xcode Previews: What is it, and how to use it

Xcode Preview has been through many updates and iterations. Learn what we can do in the latest version.

← Home