0
0
Swiftprogramming~10 mins

Custom result builder declaration in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom result builder declaration
Start: Define @resultBuilder Struct
Implement buildBlock and other methods
Use builder in function with @resultBuilder attribute
Call function with closure using builder syntax
Builder methods combine closure parts
Return combined result
This flow shows how to define a custom result builder, use it in a function, and how the builder methods combine closure parts into a final result.
Execution Sample
Swift
import Foundation

@resultBuilder
struct MyBuilder {
    static func buildBlock(_ components: String...) -> String {
        components.joined(separator: ", ")
    }
}

func makeSentence(@MyBuilder _ content: () -> String) -> String {
    content()
}

let sentence = makeSentence {
    "Hello"
    "world"
    "from"
    "Swift"
}
print(sentence)
This code defines a custom result builder that joins strings with commas, then uses it to build a sentence from multiple string lines.
Execution Table
StepActionInput to buildBlockResult
1Call makeSentence with closure["Hello", "world", "from", "Swift"]Calls buildBlock with these strings
2Inside MyBuilder.buildBlockcomponents = ["Hello", "world", "from", "Swift"]"Hello, world, from, Swift"
3makeSentence returnsResult from buildBlock"Hello, world, from, Swift"
4Print resultOutput stringHello, world, from, Swift
💡 All closure strings combined by buildBlock and returned as final string
Variable Tracker
VariableStartAfter buildBlockFinal
componentsN/A["Hello", "world", "from", "Swift"]["Hello", "world", "from", "Swift"]
resultN/AN/A"Hello, world, from, Swift"
Key Moments - 2 Insights
Why does buildBlock receive multiple strings as an array?
Because the closure passed to makeSentence contains multiple string expressions, the builder collects them as an array parameter in buildBlock (see execution_table step 2).
What happens if you don't mark the struct with @resultBuilder?
The compiler won't treat the struct as a builder, so the special syntax in the closure won't work and you'll get errors (the flow in concept_flow depends on @resultBuilder).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the input to buildBlock at step 2?
A"Hello, world, from, Swift"
B["Hello", "world", "from", "Swift"]
CAn empty array
DA single string "Hello"
💡 Hint
Check the 'Input to buildBlock' column in execution_table row 2
At which step does makeSentence return the combined string?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column for when makeSentence returns in execution_table
If you add another string "!" inside the closure, how does the buildBlock input change?
AIt includes the new string in the array
BIt becomes a single concatenated string
CIt stays the same
DIt causes a compile error
💡 Hint
Refer to how buildBlock collects all closure expressions as an array (execution_table step 2)
Concept Snapshot
@resultBuilder struct MyBuilder {
  static func buildBlock(_ components: String...) -> String {
    components.joined(separator: ", ")
  }
}

Use @MyBuilder in function parameter to build combined result from closure lines.
The builder collects closure parts and merges them in buildBlock.
Full Transcript
This example shows how to create a custom result builder in Swift. First, you define a struct marked with @resultBuilder. Inside, you implement static methods like buildBlock that take parts of the closure and combine them. Then, you use this builder in a function parameter marked with @MyBuilder. When you call the function with a closure containing multiple strings, the builder collects these strings into an array and joins them with commas. The final combined string is returned and printed. This process helps write cleaner code by letting you write multiple expressions inside a closure that get combined automatically.