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 — lesson 3 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.
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.
Frequently asked questions
Is the “Practical examples without UI frameworks” lesson free?
Yes — the full text of “Practical examples without UI frameworks” 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 “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; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
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