Swift Result Builders for Fun and Profit by Reda Lemeden

February 22, 2023

In the Swift Result Builders for Fun and Profit presentation by Reda Lemeden, he explores the use of result builders in Swift for building Domain Specific Languages (DSLs) and custom types. Result builders, introduced in Swift 5.4, are designed for building DSLs and allow developers to create complex results step by step. The speaker provides examples of using result builders for string manipulation, HTTP request building, and shape drawing. He also discusses the benefits of using result builders, such as flexibility, error handling, and the ability to create more controlled and expressive APIs. The speaker also touches upon the limitations of result builders and the learning curve associated with using them. Throughout the presentation, Reda Lemeden emphasizes the importance and potential of result builders in Swift development and encourages further exploration through resources like the official Swift programming language book and the “awesome result builders in Swift” repository.

Swift Result Builders for Fun and Profit: A comprehensive overview

What are Swift Result Builders?

Swift result builders, introduced in Swift 5.4, are a powerful feature designed to facilitate the creation of DSLs. They allow developers to construct complex results through a sequence of steps, leveraging Swift’s expressive syntax. While Swift isn’t a purely functional language, it supports functional programming features, making it an excellent choice for building DSLs and other specialized APIs.

 

Building a Domain Specific Language (DSL) in Swift

Reda Lemden began by explaining how result builders can be used to build a DSL in Swift. He illustrated this with a simple string builder example, demonstrating how result builders allow developers to concatenate strings and handle different data types like integers. This capability is crucial for creating flexible and reusable code. Reda also highlighted the importance of defining domains and supporting specific constructs like if-else statements within a result builder.

 

Implementing result builders

The talk covered the technical implementation of result builders, including how to:

  1. Define a result builder: Using the @resultBuilder attribute, developers can create a struct that handles different types and outputs a cohesive result.
  2. Handle multiple data types: By implementing multiple buildExpression functions, a result builder can manage various input types, such as strings and integers.
  3. Use build final results: This feature allows the function output to vary, supporting complex data transformations and outputs like strings or enums.

 

Practical applications: HTTP request builders and more

One of the standout examples was a HTTP request builder named requestBuilder. Reda demonstrated how result builders could simplify error handling and modularize the construction of HTTP requests. By defining various transforms (e.g., adding headers, setting request methods), developers can create flexible and reusable components. The use of a path builder further enhanced the flexibility, allowing the construction of cleaner and more maintainable URL paths.

 

Advanced features and considerations

Reda also discussed more advanced topics, such as:

  • Path builders: Used to streamline URL path creation by joining strings with slashes.
  • Validation and logic: Result builders can incorporate validation logic, making them useful for tasks like URL validation or regex building.
  • Handling complex data structures: The use of features like buildPartialBlock can manage arrays and generics, crucial for building components like drawable shapes or graphical elements.

 

Challenges and community contributions

Despite the benefits, Reda acknowledged some challenges with result builders, particularly around learning curves and platform-specific issues (e.g., needing to restart the compiler on non-Mac OS systems). He noted that while result builders enforce structure and prevent certain types of logic from being written, they can be less intuitive than traditional coding methods.

Reda highlighted several community contributions, such as libraries for building GraphQL queries, which make working with result builders more efficient and type-safe. He also pointed to popular Swift libraries like SwiftyUI and Swift Charts, which have leveraged result builders to enhance their APIs.

 

Conclusion and resources

Reda concluded the talk by encouraging developers to explore result builders as a means of simplifying and structuring their Swift code. He recommended resources like the official Swift programming language book and the “awesome result builders in Swift” repository for further learning. He emphasized the value of understanding the thought process behind these features and the iterative nature of open language design.

Swift result builders offer a unique and powerful way to create expressive and efficient code in Swift. Whether you’re building DSLs, simplifying API design, or exploring new ways to manage data transformations, result builders can significantly enhance your development process. For those looking to dive deeper, exploring the resources and community contributions highlighted in Reda’s talk is an excellent next step.

 

Additional resources

Check out more from the MeetUp Func Prog Sweden. Func Prog Sweden is the community for anyone interested in functional programming. At the MeetUps the community explore different functional languages like Erlang, Elixir, Haskell, Scala, Clojure, OCaml, F# and more.