Mon, 14 Jul 2025 19:04:00 +0000

Swift Result Builders: Creating Your Own DSL

TLDR;

Learn how to create your own Domain-Specific Language (DSL) in Swift using result builders, enhancing code readability and maintainability.

Swift's introduction of result builders has opened up exciting possibilities for developers looking to craft custom domain-specific languages (DSLs). 🚀 This feature allows us to build complex structures with a clean and intuitive syntax. In this post, I'll walk you through the process of creating your own DSL using Swift result builders, drawing from my personal experience.

Context/Motivation

When working on projects that require repetitive patterns or configurations, it's common to find yourself writing boilerplate code. This not only makes the code harder to read but also increases the likelihood of errors. To address this, I decided to explore Swift's result builders as a way to create a DSL that could simplify these tasks.

Technical Concepts

Result builders in Swift are powerful tools for constructing complex data structures from simpler components. They allow you to define custom syntax and control how elements are combined. Here’s a quick rundown of the key concepts:

@resultBuilder: A protocol that enables the creation of result builders.

buildBlock: Handles the combination of multiple elements.

buildExpression: Deals with single expressions.

buildOptional, buildEither, etc.: Handle optional and conditional logic.

Swift Code Examples

Let's dive into some code to see how this works in practice. We'll create a simple DSL for building HTML-like structures.

@resultBuilder
struct HTMLBuilder {
    static func buildBlock(_ components: Component...) -> [Component] {
        components.flatMap { $0 }
    }

    static func buildExpression(_ component: Component) -> [Component] {
        [component]
    }

    static func buildOptional(_ component: Component?) -> [Component] {
        component?.isEmpty == false ? [component!] : []
    }
}

protocol Component {
    var description: String { get }
}

struct Tag: Component {
    let name: String
    let content: [Component]

    var description: String {
        "<\(name)>\(content.map { $0.description }.joined())</\(name)>"
    }
}

struct Text: Component {
    let value: String

    var description: String {
        value
    }

    init(_ value: String) {
        self.value = value
    }
}

Now, using our HTMLBuilder, we can create HTML structures with ease:

let html = HTMLBuilder.build {
    Tag(name: "div") {
        Text("Hello, World!")
        Tag(name: "span") {
            Text("This is a span.")
        }
    }
}

print(html.map { $0.description }.joined())

Common Pitfalls

When creating DSLs with result builders, there are a few common pitfalls to watch out for:

Overcomplicating the Syntax: Keep your DSL simple and intuitive. The goal is to reduce complexity, not add to it.

Ignoring Error Handling: Ensure that your builder handles errors gracefully, especially when dealing with optional components.

Performance Considerations: Be mindful of performance implications, particularly if your DSL constructs large data structures.

Key Takeaways

Creating a DSL using Swift's result builders can significantly enhance the readability and maintainability of your code. By abstracting repetitive patterns into a custom syntax, you not only make your code cleaner but also reduce the likelihood of errors. Remember to keep your DSL simple, handle errors gracefully, and consider performance implications.

I hope this post has inspired you to explore the possibilities of result builders in Swift. Happy coding! 🎉