Swift 5.9 Features to Keep an Eye Out For

TribalScale Inc.
TribalScale
Published in
4 min readNov 21, 2023

Written by: Mat Ruschel, Sr Agile Software Engineer, TribalScale

📫 Subscribe to receive our content here.

💬 Have any questions about our development capabilities? Click here to chat with one of our experts!

Intro

In this release of Swift, some highly anticipated features have been added, while we’re not citing all of them in this blog post, we’re mentioning the ones we’re most excited about: a new macro system to enhance the expressiveness of libraries, parameter packs for simplifying the use of overloaded APIs, and changes to how you can easily use if and switch commands.

Image of Swift coding on a computer screen
Photo by Alexander Sinn on Unsplash

Macros

Macros are tools that enable developers to reduce redundant code and create more expressive libraries that can be shared as Swift packages.

Using macros is straightforward and natural. Macros can be expanded using a function-like #macroName syntax:

Alternatively, they can be associated with declarations using the @MacroName attribute:

Macros function much like built-in language features but are distinct and easily recognizable as specialized code constructs.

Macros are created using a powerful and flexible approach. They are essentially Swift functions that utilize the SwiftSyntax library to generate code that can be inserted directly into the source file. They simplify the adoption of powerful capabilities for library users, such as the new Observation module, which allows Swift classes to automatically notify other code when their properties change.

Suppose you want to create a custom logging framework for your Swift project. You can define a macro to simplify the logging calls. Instead of writing out full log statements, you can create a macro that generates the necessary logging code for you.

First, define the macro in a separate module:

Next, include the macro in your list of provided macros in your main target:

Now, you can use the macro to simplify your logging calls:

Finally, use the macro to log messages with ease:

With this macro, you no longer need to write the full print statements for logging. Instead, you can use the Log macro to generate the necessary code.

Parameter Packs

Parameter packs enable the creation of generic types and functions that can operate on any number of types.

For instance, let’s say you’re working with a set of different shapes in a drawing application, and you want to create a function that can calculate the total area of those shapes. You might have various shapes defined like this:

Now, you want to calculate the total area of these shapes, regardless of the number of shapes provided. You can use a parameter pack to achieve this:

In this example, the calculateTotalArea function takes a variable number of shapes as its parameters, and it uses a parameter pack to iterate through each shape and calculate its area. Regardless of whether you provide circles, rectangles, triangles, or any combination of these shapes, the function will correctly calculate the total area.

For example, you can use the function like this:

This illustrates how parameter packs can be used to work with different types in a flexible and elegant manner.

If and Switch Commands

Swift 5.9 also introduces smaller improvements to the language. For example, you can now use if and switch as expressions for variable assignment and return values, this helps decrease the syntax of the language a little bit.

Suppose you are developing a simple discount calculation function based on a customer’s purchase amount. Depending on their loyalty level, you want to calculate the applicable discount. You can use if and switch expressions to achieve this:

In this example, the calculateDiscount function uses if and switch expressions to determine the discount based on the customer’s loyalty rating and the purchase amount. Depending on the customer rating, the function uses the corresponding switch expression to calculate the discount percentage. This showcases how if and switch expressions can be used in combination to make your code more concise and expressive.

Next Steps

If you’re interested in looking into more features available in Swift 5.9, check the official Swift website: https://www.swift.org/

Matheus is an Agile Software Engineer at TribalScale. He is passionate about following technology and discovering new tools that make his work more efficient. He also enjoys reading about the economy and investments, and in his free time he is a big gym rat and loves keeping healthy.

TribalScale is a global innovation firm that helps enterprises adapt and thrive in the digital era. We transform teams and processes, build best-in-class digital products, and create disruptive startups. Learn more about us on our website. Connect with us on Twitter, LinkedIn & Facebook!

--

--

TribalScale Inc.
TribalScale

A digital innovation firm with a mission to right the future.