Challenge - 5 Problems
Result Builder DSL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Look at how the buildBlock joins components with new lines and how the html function wraps the content.
✗ Incorrect
The result builder collects the strings and joins them with new lines. The html function adds tags with new lines around the content, so the output preserves line breaks.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about how result builders let you write code that looks like natural language or markup.
✗ Incorrect
Result builders let you write nested blocks that the compiler transforms into combined results, making DSLs look clean and readable.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check the parameter types in buildBlock and what the closure returns.
✗ Incorrect
The buildBlock expects parameters of type String but receives [String], causing a type mismatch error.
❓ Predict Output
advanced2: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)
Attempts:
2 left
💡 Hint
Look at how buildEither handles the if-else and how buildBlock joins strings.
✗ Incorrect
The if condition is true, so buildEither(first:) returns "morning". The buildBlock joins all strings with spaces, producing "Good morning everyone!".
🧠 Conceptual
expert1:30remaining
How do result builders improve code readability in DSLs?
Which statement best describes how result builders improve readability when creating DSLs in Swift?
Attempts:
2 left
💡 Hint
Think about how DSLs look like natural language or markup with result builders.
✗ Incorrect
Result builders allow writing nested blocks that resemble the DSL's structure, hiding complexity and improving readability.