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 — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Building mini-DSLs with result builders” lesson free?
Yes — the full text of “Building mini-DSLs with result builders” is free to read here on the web, and the Swift Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
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