Skip to content

Conversation

@calda
Copy link
Contributor

@calda calda commented Dec 31, 2025

Corresponding compiler PR: swiftlang/swift#86244


This PR prototypes support for [Element] { ... } trailing closure init syntax for arrays and dictionaries.

Today, if you have an @ArrayBuilder type and define:

extension Array {
  init(@ArrayBuilder<Element> _ build: () -> [Element]) {
    self = build()
  }
}

you currently cannot call it like this:

let myArray = [String] { // error: 'let' declarations cannot be computed properties
  "foo"
  "bar"
}

This PR enables support for that syntax by allowing trailing closures following array literals.

@calda calda force-pushed the cal--array-init-trailing-closure branch from 66302df to ccb722a Compare December 31, 2025 21:34
Comment on lines +2257 to +2258
assertParse("_ = [1] { return [1] }")
assertParse("_ = [1: 1] { return [1: 1] }")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These now parse, but fail type checking in the full compiler:

  _ = [1] { return [1] } // expected-error {{cannot call value of non-function type '[Int]'}}
  _ = [1: 1] { return [1: 1] } // expected-error {{cannot call value of non-function type '[Int : Int]'}}

  _ = [1] // expected-error {{cannot call value of non-function type '[Int]'}}
  { return [1] }

  _ = [1: 1] // expected-error {{cannot call value of non-function type '[Int : Int]'}}
  { return [1: 1] }

@calda calda changed the title Support [Element] { ... } trailing closure init syntax Array expression trailing closures Jan 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant