Collection Blocks
A call can end with a block. When the callee's final parameter is an array, the block supplies that parameter. This is a general language feature, not a UI feature.
Page(padding: 20) {
Heading("Today")
Text("Two open tasks")
}is the same call as:
Page(padding: 20, children: [
Heading("Today"),
Text("Two open tasks"),
])When the callee's final parameter is not an array, the older child-sink block form remains for builder compatibility. New library APIs should take an array parameter.
Elements
- Each expression statement contributes one element, in order.
if,else,for, andwhilecontribute the elements of the branch or iteration they take.switcharms follow the normal switch rules; each arm still needs its explicit terminator.letandconstare scoped to the block and contribute nothing.returncannot leave a collection block. It isE108.- A throwing call, a throwing constructor, or
?cannot appear in a collection block. Hoist it above the block or handle it outside. All areE108. breakandcontinuemay target a loop inside the block. Leaving the collection block with either isE108....itemssplices an existing array into the current collection.name: { ... }supplies a named array parameter. Braces are required.
List {
Header {
Text(openCount.toString() + " open")
Spacer()
Button("Clear", clear)
}
for task in tasks {
TaskRow(task)
}
}Slots
A slot is a named array parameter. It is useful when a component has more than one region.
Sheet(detents: [0.0, 280.0, 520.0]) {
title: { Heading("Details") }
Text("Body")
actions: {
Button("Close", close)
Button("Save", save)
}
}The unnamed statements fill children. title: and actions: fill those
parameters, which must also be arrays. Duplicate slot names and passing the
same parameter in the call head and the block are E108.
Type rules
- The callee's final parameter must be
Array<T>. - Every element must have type
T.Anyis accepted only forArray<Any>. - A spread value must be
Array<T>. - A slot's element type comes from that slot's parameter, not from
children. - A slot that does not name an array parameter is
E108. - Type errors name the actual and expected element types.
Current limits
A method chain after a trailing block and a nested trailing block both parse.
Collection-block detection covers free functions, static factories, instance
methods on a typed local, and this.method. Generic array parameters are
rejected until callee type-argument substitution lands.
For library authors
Declare the array parameter last and give it a default:
fn Stack(
gap: Int = 8,
children: Array<View> = [],
): ViewCallers can then write Stack { ... } or Stack(gap: 12) { ... }. The block
is evaluated in the caller's scope. There is no receiver and no hidden builder
state. The callee receives a normal array.