Building mini-DSLs with result builders
Build a tiny DSL using @resultBuilder : define builder functions, compose blocks, and support conditionals/loops for readable construction.
Building mini-DSLs with result builders is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 3 lessons in total.
Why result builders?
Goal: Turn readable blocks into values using @resultBuilder. You define how items combine; the call site stays clean and declarative.
A tiny builder
We declare buildBlock to join items and expose an API (lines{}) that returns an array of strings from a clean block.
@resultBuilder
struct StringListBuilder {
static func buildBlock(_ parts: String...) -> [String] { parts }
// Optional support for conditionals/arrays:
static func buildEither(first: [String]) -> [String] { first }
static func buildEither(second: [String]) -> [String] { second }
static func buildArray(_ components: [[String]]) -> [String] { components.flatMap { $0 } }
}
// DSL entry function using the builder
func lines(@StringListBuilder _ content: () -> [String]) -> [String] {
content()
}
// Usage: create an array of strings with a block
let list = lines {
"Hello"
"Swift"
"Builders"
}
print(list)Conditionals in builders
With buildEither, if/else works inside the block. The builder decides which branch contributes.
let showExtra = true
let conditional = lines {
"Base"
if showExtra {
"Extra"
} else {
"Hidden"
}
}
print(conditional) // ["Base","Extra"]Loops in builders
buildArray gathers results from loops (for/map), so repeated pieces can be emitted naturally.
let items = ["a","b","c"]
let looped = lines {
for x in items { x.uppercased() } // contributes ["A","B","C"]
}
print(looped)Single-value builder
Builders can output a single value. Here we join parts into one string, still using conditionals.
@resultBuilder
struct TextBuilder {
static func buildBlock(_ parts: String...) -> String {
parts.joined(separator: " ")
}
static func buildEither(first: String) -> String { first }
static func buildEither(second: String) -> String { second }
static func buildArray(_ components: [String]) -> String {
components.joined(separator: " ")
}
}
func text(@TextBuilder _ content: () -> String) -> String { content() }
// Compose a small sentence with conditions
let loggedIn = false
let sentence = text {
"[INFO]"
"Welcome,"
if loggedIn { "user!" } else { "guest." }
}
print(sentence) // "[INFO] Welcome, guest."Design tips
Tips:
- Keep DSLs minimal and focused.
- Provide one clear entry function annotated with your builder.
- Support only what you need:
buildBlock,buildEither,buildArray. - Document what statements are allowed inside the block.
Result builder purpose
Quick check: What is the main benefit of a result builder?
Recap
Recap: Define @resultBuilder methods to combine pieces, expose a tiny entry API, and compose readable blocks with if/for support.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Building mini-DSLs with result builders” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Building mini-DSLs with result builders”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 3 lessons in total.
What will I learn in “Building mini-DSLs with result builders”?
Build a tiny DSL using @resultBuilder : define builder functions, compose blocks, and support conditionals/loops for readable construction. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.
How long does the “Building mini-DSLs with result builders” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Building mini-DSLs with result builders
- Scoped APIs & readability patterns
- Practical examples without UI frameworks