3 Cool new features of Swift 5.9 šŸ˜

Now also in Swiftā€¦

Sagar
4 min readSep 4, 2023

01. Using if and switch as expressions

Until Swift 5.9, the keywords if and switch could only be used as statements.

This meant that it wasnā€™t possible to directly assign the result of an if or a switch to a variable or to pass it as an argument.

if and switch can now be used as expressions!

This means that these awful nested ternary expressions can now be replaced by much more readable if conditions:

Even better, this new feature also works with conditional binding:

02. Parameter Packs

If youā€™ve ever had to write code that needed to handle an unknown number of generic types, youā€™ve probably learned the hard way that Swift didnā€™t offer support for variadic generics.

And that the best you could do would be to implement multiple overloads, that would handle up to a certain number of generic arguments:

And even though this approach looks a bit ridiculous, major first party frameworks like SwiftUI and Combine used to rely on this trick!

Unfortunately, it has a major drawback: since you canā€™t implement an infinite number of overloads, there will always be a limit on the number of arguments your function can handle:

Have you ever wondered why a SwiftUI view couldnā€™t support more than 10 child views? Now you know the answer!

But if you open Xcode 15 and try to add 11 child to a SwiftUI view, you will notice that this limitation is now gone.

This is thanks to a new feature of Swift 5.9 called Parameter Packs:

Using the keyword each, we are able to declare a Parameter Pack.

And then, through the keyword repeat we are able to use this Parameter Pack, for instance to declare the arguments of a function or to define a tuple type:

Thanks to this new syntax, our function evaluate() is now able to handle any arbitrary number of arguments.

However, be careful: thereā€™s a reason why this feature is called Parameter Packs and not Variadic Generics!

Itā€™s because how you handle a Parameter Pack is still a bit limited: for instance, itā€™s not yet possible to iterate over each individual argument the function will receive.

#03 ā€” Macros

Back in 2019, Swift had introduced Property Wrappers, which enabled us to nicely encapsulate many boilerplate codes.

This year, the language goes even further with the introduction of Macros, which are nothing less than small compiler plugins capable of generating code on your behalf.

Hereā€™s the typical boilerplate code that a macro can generate for you: defining a convenience computed property for each of the cases in an enum.

In Swift 5.9 you can remove this boilerplate code and instead implement a macro called @CaseDetection:

At compile time, this macro will then be expanded by the Swift compiler in order to produce the generated code:

Thatā€™s it: these were the 3 new features of Swift 5.9, I wanted to introduce you to..!!!

I wish you an amazing week! šŸ™Œ

Rejection is better than fake promisesā€¦ šŸ¤”

--

--