Monday, October 15, 2018

Building DSLs in Swift

John Sundell:

So let’s take things one step further, and enable our above code to be used as a proper DSL. The first thing we’ll need is an execution context. One reason that DSLs can remove so much verbosity and cruft, is that they’re used in a very specific context, that itself already provides much of the information required to understand what the code does.

[…]

For our context, we’ll take some inspiration from the UIView.animate API, and use a closure to encapsulate the usage of our DSL. All we need to make that happen is a simple extension on UIView that adds a method that in turn calls our context closure.

[…]

Let’s see how we can improve our DSL using operators - starting with overloading the plus and minus operators to enable us to combine a layout anchor and a constant into a tuple - which’ll later let us act on them as one unit[…]

Comments RSS · Twitter

Leave a Comment