General purpose programming languages (GPLs), like Swift, are great tools that can be used to implement almost any kind of software. However, this flexibility often comes with a cost. Particularly for problem domains that are characterised by a very strong structure, you will find that GPLs are not ideal.
If you've ever run into this situation, I recommend checking out Apple's WWDC 21 session about building DSLs with Swift:
Some problems are easier to solve by creating a customized programming language, or “domain-specific language.”
I couldn't put this any better. DSLs are very powerful if used correctly, and can elevate developer productivity on a complete new level.
The most well-known DSL written in Swift is SwiftUI itself (Apple introduced a ton of features to the Swift language to make this possible), and it has made building UIs a lot easier. For a more self-contained example, check out issue issue #31, in which I talked about Setting - a DSL for building Settings screens for iOS apps.
h/t to Pitt for sharing this video!
We all know feature flags - either because we use them in our own apps, or (and this is likely more common) because we use apps that make use of feature flags. Usually, developers will put new features behind a feature flag, and then gradually roll it out to users of their app.
For the Swift compiler, Apple has implemented something similar, but instead of controlling it from their end (which would be a really bad experience for developers), we get to enable new and upcoming features as we see fit.
In his article, Marcel Voss explains how you can use this to enable some of the upcoming Swift 6 features in the Swift 5.8 compiler.
I love the title of the Swift Evolution proposal that made this possible: SE-0362: Piecemeal adoption of upcoming language improvements - it refers to one of the reasons why the Swift team decided to build this feature:
- Enabling developers to use new features sooner rather than waiting for Swift 6 to ship
- Gathering feedback from the community, and enabling the team to fine-tune the features
- Enabling developers to gradually adopt new features over time, instead of having to migrate large chunks of their code when Swift 6 is released