0
0
Swiftprogramming~20 mins

Why result builders enable DSLs in Swift - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Result Builder DSL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift result builder example?
Consider this Swift code using a result builder to create a simple HTML DSL. What will be printed when running this code?
Swift
import Foundation

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

func html(@HTMLBuilder content: () -> String) -> String {
    "<html>\n" + content() + "\n</html>"
}

let page = html {
    "<head><title>Test</title></head>"
    "<body><h1>Hello, DSL!</h1></body>"
}

print(page)
A
&lt;html&gt;
&lt;head&gt;&lt;title&gt;Test&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Hello, DSL!&lt;/h1&gt;&lt;/body&gt;
&lt;/html&gt;
B<html><head><title>Test</title></head><body><h1>Hello, DSL!</h1></body></html>
CCompilation error: Missing return in function
D
&lt;html&gt;
&lt;head&gt;&lt;title&gt;Test&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;&lt;h1&gt;Hello, DSL!&lt;/h1&gt;&lt;/body&gt;
&lt;/html&gt;
Attempts:
2 left
💡 Hint
Look at how the buildBlock joins components with new lines and how the html function wraps the content.
🧠 Conceptual
intermediate
1:30remaining
Why do result builders simplify DSL creation in Swift?
Which of the following best explains why result builders enable easy creation of DSLs in Swift?
AThey replace all functions with macros at compile time.
BThey enforce strict type checking preventing any implicit conversions.
CThey allow writing nested code blocks that automatically combine results without explicit function calls.
DThey require manual string concatenation for every DSL element.
Attempts:
2 left
💡 Hint
Think about how result builders let you write code that looks like natural language or markup.
🔧 Debug
advanced
2:00remaining
Identify the error in this Swift result builder code
This Swift code using a result builder fails to compile. What is the cause?
Swift
@resultBuilder
struct ListBuilder {
    static func buildBlock(_ components: [String]...) -> [String] {
        components.flatMap { $0 }
    }
}

func list(@ListBuilder content: () -> [String]) -> [String] {
    content()
}

let items = list {
    ["Apple", "Banana"]
    ["Cherry"]
}

print(items)
AError: Function 'list' missing @escaping attribute
BError: Cannot convert value of type '[String]' to expected argument type 'String' in buildBlock
CError: Missing return statement in buildBlock
DNo error, prints ["Apple", "Banana", "Cherry"]
Attempts:
2 left
💡 Hint
Check the parameter types in buildBlock and what the closure returns.
Predict Output
advanced
2:00remaining
What is the output of this Swift result builder with conditional logic?
Given this Swift code using a result builder with an if statement, what will be printed?
Swift
@resultBuilder
struct TextBuilder {
    static func buildBlock(_ components: String...) -> String {
        components.joined(separator: " ")
    }
    static func buildEither(first component: String) -> String {
        component
    }
    static func buildEither(second component: String) -> String {
        component
    }
}

func text(@TextBuilder content: () -> String) -> String {
    content()
}

let isMorning = true

let greeting = text {
    "Good"
    if isMorning {
        "morning"
    } else {
        "evening"
    }
    "everyone!"
}

print(greeting)
AGood morning everyone!
BGood evening everyone!
C
Good
morning
everyone!
DCompilation error: Missing buildEither methods
Attempts:
2 left
💡 Hint
Look at how buildEither handles the if-else and how buildBlock joins strings.
🧠 Conceptual
expert
1:30remaining
How do result builders improve code readability in DSLs?
Which statement best describes how result builders improve readability when creating DSLs in Swift?
AThey let developers write nested, declarative code that looks like the target language, hiding boilerplate and combining results automatically.
BThey force all DSL code to be written as flat functions without nesting, improving clarity.
CThey require explicit calls to combine each DSL element, making the code verbose but clear.
DThey convert all DSL code into JSON at compile time for better readability.
Attempts:
2 left
💡 Hint
Think about how DSLs look like natural language or markup with result builders.