Practical examples without UI frameworks
Write small, useful DSLs without UI: config files, simple query builders, text/Markdown emitters, and validation pipelines with @resultBuilder .
Practical examples without UI frameworks is a free Swift Academy lesson on CoddyKit. This is lesson 3 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.
Non-UI result builders
You can build handy DSLs for many tasks beyond UI. We will craft config, query, text, and validation builders. Keep each one tiny and focused.
Config builder
A config builder collects simple tuples into a dictionary. Call sites are neat and easy to scan.
@resultBuilder
struct ConfigBuilder {
static func buildBlock(_ parts: (String,String)...) -> [String:String] {
var dict: [String:String] = [:]
for (k,v) in parts { dict[k] = v }
return dict
}
}
func config(@ConfigBuilder _ body: () -> [String:String]) -> [String:String] { body() }
// Use it:
let cfg = config {
("ENV", "debug")
("API_URL", "https://api.example.com")
("RETRY", "3")
}
print(cfg["API_URL"] ?? "missing")Query builder
A query builder joins clauses with AND. We support if/else and loops via builder hooks.
struct Where { let clauses: [String] }
@resultBuilder
struct WhereBuilder {
static func buildBlock(_ parts: String...) -> Where { Where(clauses: parts) }
static func buildArray(_ parts: [String]) -> Where { Where(clauses: parts) }
static func buildEither(first: Where) -> Where { first }
static func buildEither(second: Where) -> Where { second }
}
func whereClauses(@WhereBuilder _ body: () -> Where) -> Where { body() }
func select(_ table: String, where w: Where) -> String {
let filter = w.clauses.isEmpty ? "" : " WHERE " + w.clauses.joined(separator: " AND ")
return "SELECT * FROM \(table)\(filter);"
}
// Use:
let isProd = false
let q = select("users", where: whereClauses {
"age > 18"
if isProd { "status = 'active'" } else { "status <> 'deleted'" }
})
print(q)Text/Markdown builder
A text builder emits a single String. Helpers like h2 and bullet keep the block readable.
@resultBuilder
struct TextBuilder {
static func buildBlock(_ parts: String...) -> String { parts.joined(separator: "\n") }
static func buildEither(first: String) -> String { first }
static func buildEither(second: String) -> String { second }
static func buildArray(_ parts: [String]) -> String { parts.joined(separator: "\n") }
}
func md(@TextBuilder _ body: () -> String) -> String { body() }
func h2(_ s: String) -> String { "## " + s }
func bullet(_ s: String) -> String { "- " + s }
let doc = md {
h2("Release Notes")
bullet("Faster build")
bullet("Smaller binary")
}
print(doc)Validation builder
Collect validation rules then run them as a pipeline. The block stays declarative; execution happens later.
struct ValidationResult { let ok: Bool; let messages: [String] }
typealias Rule = (String) -> String? // nil = OK; otherwise error message
@resultBuilder
struct RulesBuilder {
static func buildBlock(_ parts: Rule...) -> [Rule] { parts }
static func buildArray(_ parts: [[Rule]]) -> [Rule] { parts.flatMap { $0 } }
}
func rules(@RulesBuilder _ body: () -> [Rule]) -> [Rule] { body() }
func validate(_ input: String, with rules: [Rule]) -> ValidationResult {
var msgs: [String] = []
for r in rules { if let m = r(input) { msgs.append(m) } }
return ValidationResult(ok: msgs.isEmpty, messages: msgs)
}
// Use:
let rs = rules {
{ $0.isEmpty ? "Must not be empty" : nil }
{ $0.count < 3 ? "Too short" : nil }
}
let out = validate("hi", with: rs)
print(out.ok, out.messages)Design tips
Tips:
- Start with buildBlock; add either/array only when needed.
- Keep emitted types simple: String, arrays, small structs.
- Provide one entry function per DSL (e.g., config, md).
- Document allowed statements and escaping rules.
Non-UI builder use case
Quick check: Pick a solid non-UI use for a builder:
Recap
Recap: Result builders shine for structured inputs—configs, queries, text, and validation. Keep them minimal, readable, and well-scoped.
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 “Practical examples without UI frameworks” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Practical examples without UI frameworks”, 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 “Practical examples without UI frameworks”?
Write small, useful DSLs without UI: config files, simple query builders, text/Markdown emitters, and validation pipelines with @resultBuilder . 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 3 of 3.
How long does the “Practical examples without UI frameworks” 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