-
Write a DSL in Swift using result builders
Some problems are easier to solve by creating a customized programming language, or “domain-specific language.” While creating a DSL traditionally requires writing your own compiler, you can instead use result builders with Swift 5.4 to make your code both easier to read and maintain. We'll take you through best practices for designing a custom language for Swift: Learn about result builders and trailing closure arguments, explore modifier-style methods and why they work well, and discover how you can extend Swift's normal language rules to turn Swift into a DSL.
To get the most out of this session, it's helpful (though not necessary) to have some experience writing SwiftUI views. You won't need to know anything about parser or compiler implementation.Resources
- Result Builders - The Swift Programming Language
- Attributes - The Swift Programming Language
- Fruta: Building a feature-rich app with SwiftUI
Related Videos
WWDC21
WWDC19
-
Search this video…
♪ Bass music playing ♪ ♪ Becca Royal-Gordon: Hi, I’m Becca from the Swift Compiler team. I’m going to be talking today about how you can implement DSLs in Swift. If you’ve never heard that term, a DSL is a domain-specific language, and even if the name is new to you, you’ve probably used one before. I’m going to start by explaining what DSLs actually are and what they look like in Swift. Then I’ll explain how result builders work. They’re one of the main features used to implement Swift DSLs. After that, I’ll walk you through designing a simple DSL for part of our sample app, Fruta. And finally, I’ll show you how to write the implementation that’s in the Fruta sample code. But let’s start by explaining that acronym a little better. A DSL is a sort of miniature programming language that’s designed for programs that work in a specific area called a "domain." Because the language is designed with a particular kind of work in mind, it can have special features which make that kind of work easier to do. So when you write code for a DSL instead of a general-purpose language, you only have to write the things that are specific to your exact problem. Many DSLs are declarative. That is, you’re not really writing precise instructions to solve the problem; it’s more like you’re describing the problem in the language, and then it goes and solves it for you. The traditional way you would do this is called a "standalone DSL." You would design the entire language from scratch and write an interpreter or compiler for it. Embedded DSLs are a more modern alternative. In an embedded DSL, you use the built-in features of a host language like Swift to add the DSL’s implicit behavior to some parts of your code, effectively modifying the host language into one tailored for your domain. This is obviously way easier than designing the entire language and writing a compiler for it because you’re starting from an existing language that has already decided the basics of the syntax and already has a compiler. It also makes it easier to mix the DSL code together with n