Sketchnote summary for Swift language announcements from WWDC22 blog post

Swift has evolved significantly during the past year, and we’ve seen two large updates to the language. Swift 5.6 was released in March 2022 and introduced major improvements to the type system, concurrency model and Swift ecosystem. It laid the groundwork for further updates in Swift 5.7, which is included in beta versions of Xcode 14 and available on the Swift.org downloads page.

Many Swift updates were rightfully celebrated during WWDC22, with sessions focusing on language changes, tooling improvements, additions to Swift packages and more. The What’s new in Swift video provides a great overview of Swift news for the past year. In this post we would like to share our highlights about the Swift ecosystem from WWDC.

Community initiatives

The community around the Swift language is growing, and the great initiatives introduced earlier are expanding and getting more widespread support. New workgroups have been announced that will be focusing on specific areas in the Swift ecosystem. The Swift website workgroup is guiding the evolution of the Swift.org website, the Swift and C++ interoperability group is working on advancing the interoperability support between the two languages, and the Swift language workgroup is overseeing the language and standard library. The Swift Mentorship Program, announced in 2021, has been extended for another year and includes additional topics, such as DocC, C++ interoperability and the Swift website. It’s worth mentioning that the Swift.org website is now open source and ready for community contributions.

Speaking of open source, the Swift-DocC compiler for Swift frameworks and packages announced last year is now open source too, and has some great improvements. It added support for app projects as well as support for Objective-C and C API documentation. We definitely recommend checking out the What’s new in Swift-DocC session to learn more.

Swift packages

We’ve seen important updates to the Swift Package Manager, including support for module disambiguation, the ability to define build tool plugins and custom command plugins, as well as security and performance improvements.

Starting from Swift 5.6, Swift Package Manager performs trust on first use (TOFU) validation. The fingerprint of a package is recorded when the package is first downloaded and subsequent downloads will report an error if the fingerprint is different. This change is great for better security of packages.

In Swift 5.7, Swift Package Manager gained an exciting improvement, that will help us avoid problems with using multiple packages with the same name in our projects. It now lets us name modules from outside the packages that define them, and add module aliases.

With the new PackagePlugin API, we can define custom plugins to generate source code or automate release tasks. The Create Swift Package plugins session teaches us how to write such plugins in Swift.

Language updates

This year we’ve seen some great additions to the Swift language that include a whole spectrum of changes, from smaller syntax improvements to larger generics and concurrency updates.

This post highlights some of the Swift updates that caught our attention this WWDC. For the code samples in the following sections, we took inspiration from the Bird of the Year competition in Aotearoa New Zealand. After all, given who the winner of Bird of the Year 2021 was (it was a bat 🤫), some additional type safety for contestants couldn’t hurt.

Quality of life improvements

One small but really nice improvement in Swift 5.7 is the shorthand syntax for optional unwrapping with if letguard let and while let. We can drop the right-hand side to get the unwrapped optional with the same name as the original.

<span class="k">var</span> <span class="nv">startDate</span><span class="p">:</span> <span class="kt">Date</span><span class="p">?</span>

<span class="k">if</span> <span class="k">let</span> <span class="nv">startDate</span> <span class="p">{</span>
    <span class="nf">print</span><span class="p">(</span><span class="s">"""
    Bird of the Year competition </span><span class="err">\</span><span class="s">
    starts on </span><span class="se">\(</span><span class="n">startDate</span><span class="o">.</span><span class="nf">formatted</span><span class="p">()</span><span class="se">)</span><span class="s">
    """</span><span class="p">)</span>
<span class="p">}</span>

Another change that will improve our code is type inference support for complex closures with multiple statements. We no longer need to specify the return type manually if our closure contains if elsedo catch or any other control flow statements.

<span class="k">let</span> <span class="nv">participants</span> <span class="o">=</span> <span class="p">[</span><span class="s">"Kororā"</span><span class="p">,</span> <span class="s">"Weka"</span><span class="p">,</span> <span class="s">"Pekapeka-tou-roa"</span><span class="p">]</span>

<span class="k">let</span> <span class="nv">introductions</span> <span class="o">=</span> <span class="n">participants</span><span class="o">.</span><span class="n">map</span> <span class="p">{</span>
    <span class="k">if</span> <span class="nv">$0</span><span class="o">.</span><span class="nf">hasPrefix</span><span class="p">(</span><span class="s">"Pekapeka"</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="s">"</span><span class="se">\(</span><span class="nv">$0</span><span class="se">)</span><span class="s"> is a mammal"</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="k">return</span> <span class="s">"</span><span class="se">\(</span><span class="nv">$0</span><span class="se">)</span><span class="s"> is a bird"</span>
    <span class="p">}</span>
<span class="p">}</span>

String processing

Swift 5.7 features huge updates for string processing with the introduction of regex literals and the RegexBuilder library, paired with matching methods and strongly typed captures.

We can name our matches and easily extract them from the final result.

<span class="k">let</span> <span class="nv">intent</span> <span class="o">=</span> <span class="s">"I vote for Pūkeko and Kea"</span>

<span class="k">let</span> <span class="nv">regex</span> <span class="o">=</span> <span class="o">/</span><span class="kt">I</span> <span class="n">vote</span> <span class="k">for</span> <span class="p">(?</span><span class="o"><</span><span class="n">bird1</span><span class="o">>.+</span><span class="p">?)</span> <span class="nf">and</span> <span class="p">(?</span><span class="o"><</span><span class="n">bird2</span><span class="o">>.+</span><span class="p">?)</span><span class="o">/</span>

<span class="k">if</span> <span class="k">let</span> <span class="nv">votes</span> <span class="o">=</span> <span class="k">try</span><span class="p">?</span> <span class="n">regex</span><span class="o">.</span><span class="nf">wholeMatch</span><span class="p">(</span><span class="nv">in</span><span class="p">:</span> <span class="n">intent</span><span class="p">)</span> <span class="p">{</span>
    <span class="nf">print</span><span class="p">(</span><span class="s">"Your first choice is </span><span class="se">\(</span><span class="n">votes</span><span class="o">.</span><span class="n">bird1</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
    <span class="nf">print</span><span class="p">(</span><span class="s">"Your second choice is </span><span class="se">\(</span><span class="n">votes</span><span class="o">.</span><span class="n">bird2</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>

And with RegexBuilder, we can construct our regex search with a SwiftUI-style language, that can make it more readable and unlock more powerful capabilities.

<span class="k">let</span> <span class="nv">word</span> <span class="o">=</span> <span class="kt">OneOrMore</span><span class="p">(</span><span class="o">.</span><span class="n">word</span><span class="p">)</span>
<span class="k">let</span> <span class="nv">regex</span> <span class="o">=</span> <span class="kt">Regex</span> <span class="p">{</span>
    <span class="s">"I vote for "</span>
    <span class="kt">Capture</span> <span class="p">{</span> <span class="n">word</span> <span class="p">}</span>
    <span class="s">" and "</span>
    <span class="kt">Capture</span> <span class="p">{</span> <span class="n">word</span> <span class="p">}</span>
<span class="p">}</span>

<span class="k">if</span> <span class="k">let</span> <span class="nv">votes</span> <span class="o">=</span> <span class="k">try</span><span class="p">?</span> <span class="n">regex</span><span class="o">.</span><span class="nf">wholeMatch</span><span class="p">(</span><span class="nv">in</span><span class="p">:</span> <span class="n">intent</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">let</span> <span class="p">(</span><span class="nv">_</span><span class="p">,</span> <span class="nv">bird1</span><span class="p">,</span> <span class="nv">bird2</span><span class="p">)</span> <span class="o">=</span> <span class="n">votes</span><span class="o">.</span><span class="n">output</span>
    <span class="nf">print</span><span class="p">(</span><span class="s">"Your first choice is </span><span class="se">\(</span><span class="n">bird1</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
    <span class="nf">print</span><span class="p">(</span><span class="s">"Your second choice is </span><span class="se">\(</span><span class="n">bird2</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>

There are two informative sessions about the new string processing APIs: Meet Swift Regex and Swift Regex: Beyond the basics.

Generics and protocols

There are many improvements to Swift protocols and generics this year. Protocols now support primary associated types. We can specify it in angle brackets next to the protocol name, and it should be the type that is used more often than others at the call site.

<span class="kd">protocol</span> <span class="kt">Contestant</span><span class="o"><</span><span class="kt">Habitat</span><span class="o">></span> <span class="p">{</span>
    <span class="kd">associatedtype</span> <span class="kt">Habitat</span><span class="p">:</span> <span class="kt">Territory</span>
    <span class="kd">associatedtype</span> <span class="kt">Food</span>

    <span class="k">var</span> <span class="nv">home</span><span class="p">:</span> <span class="kt">Habitat</span> <span class="p">{</span> <span class="k">get</span> <span class="k">set</span> <span class="p">}</span>
    <span class="k">var</span> <span class="nv">favoriteFood</span><span class="p">:</span> <span class="kt">Food</span> <span class="p">{</span> <span class="k">get</span> <span class="k">set</span> <span class="p">}</span>
    <span class="k">var</span> <span class="nv">name</span><span class="p">:</span> <span class="kt">String</span> <span class="p">{</span> <span class="k">get</span> <span class="p">}</span>
<span class="p">}</span>

Using the angle brackets syntax, we can then constrain the primary associated type to a specific type.

<span class="kd">func</span> <span class="n">fundReforestation</span><span class="o"><</span><span class="kt">Animal</span><span class="p">:</span> <span class="kt">Contestant</span><span class="o"><</span><span class="kt">Forest</span><span class="o">>></span><span class="p">(</span><span class="k">for</span> <span class="nv">animal</span><span class="p">:</span> <span class="kt">Animal</span><span class="p">)</span> <span class="p">{</span>
    <span class="nf">scheduleTreePlanting</span><span class="p">(</span><span class="nv">in</span><span class="p">:</span> <span class="n">animal</span><span class="o">.</span><span class="n">home</span><span class="p">)</span>
<span class="p">}</span>

It’s also becoming easier to write generic methods and functions thanks to the new ability to use some with parameter types. If a generic parameter is only used in one place, we can specify the protocol together with some keyword for its type, without the need to set a generic constraint on the function.

<span class="kd">func</span> <span class="nf">fundConservationEfforts</span><span class="p">(</span><span class="k">for</span> <span class="nv">animal</span><span class="p">:</span> <span class="kd">some</span> <span class="kt">Contestant</span><span class="p">)</span> <span class="p">{</span>
    <span class="nf">establishProtectedAreas</span><span class="p">(</span><span class="nv">in</span><span class="p">:</span> <span class="n">animal</span><span class="o">.</span><span class="n">home</span><span class="p">)</span>
    <span class="nf">startIntensiveMonitoring</span><span class="p">(</span><span class="nv">of</span><span class="p">:</span> <span class="n">animal</span><span class="p">)</span>
<span class="p">}</span>

We can even use some with parameters, that have primary associated type constraints, so we can rewrite our previous example for fundReforestation() function in a cleaner way.

<span class="kd">func</span> <span class="nf">fundReforestation</span><span class="p">(</span><span class="k">for</span> <span class="nv">animal</span><span class="p">:</span> <span class="kd">some</span> <span class="kt">Contestant</span><span class="o"><</span><span class="kt">Forest</span><span class="o">></span><span class="p">)</span> <span class="p">{</span>
    <span class="nf">scheduleTreePlanting</span><span class="p">(</span><span class="nv">in</span><span class="p">:</span> <span class="n">animal</span><span class="o">.</span><span class="n">home</span><span class="p">)</span>
<span class="p">}</span>

There have also been a ton of improvements for existential types in Swift. We can now use the any keyword to mark places where existential types are used, and we no longer have the constraints we had before for using existential types with Self or associatedtype requirements. We can put them into a collection, use an existential type to constrain a variable or use it as a parameter type.

For example, we can make an array of contestants, which we wouldn’t be able to do before, because Contestant protocol in our example has associated types.

<span class="k">let</span> <span class="nv">contestants</span><span class="p">:</span> <span class="p">[</span><span class="n">any</span> <span class="kt">Contestant</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">brownKiwi</span><span class="p">,</span> <span class="n">ruru</span><span class="p">,</span> <span class="n">whio</span>
<span class="p">]</span>

<span class="k">for</span> <span class="n">contestant</span> <span class="k">in</span> <span class="n">contestants</span> <span class="p">{</span>
    <span class="nf">fundConservationEfforts</span><span class="p">(</span><span class="nv">for</span><span class="p">:</span> <span class="n">contestant</span><span class="p">)</span>
<span class="p">}</span>

Knowing that previous constraints for existential types are gone, we can audit our existing code for type-erasing wrappers and check if we can reimplement them using existential types now.

To find out more about generics in Swift 5.7, make sure to check out the Embrace Swift generics session. And to learn how to design advanced abstractions using protocols, watch Design protocol interfaces in Swift.

Concurrency

Concurrency updates this year build upon last year’s changes and focus on data race safety. We even have new opt-in safety checks in Xcode to help us identify potential issues.

Swift actors, introduced last year, help us write thread-safe code by isolating access to their properties. New distributed actors this year take the notion of actor isolation further and make it simpler to develop distributed systems. The open source Distributed Actors library provides a complete solution for building server-side clustered distributed systems in Swift. The Meet distributed actors in Swift video goes into more detail on using actors when working with distributed systems and apps.

We also have a new Swift package Async Algorithms, that provides ready solutions for working with AsyncSequence. It includes algorithms that could be tricky to implement on our own such as debouncethrottlemerge, and zip. These algorithms help us to work with values over time.

<span class="k">let</span> <span class="nv">kiwiFinder</span> <span class="o">=</span> <span class="kt">KiwiFinder</span><span class="p">()</span>
<span class="k">let</span> <span class="nv">counter</span> <span class="o">=</span> <span class="kt">Counter</span><span class="p">()</span>

<span class="k">for</span> <span class="k">try</span> <span class="nf">await</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">kiwi</span><span class="p">)</span> <span class="k">in</span> <span class="nf">zip</span><span class="p">(</span><span class="n">counter</span><span class="p">,</span> <span class="n">kiwiFinder</span><span class="p">)</span> <span class="p">{</span>
    <span class="nf">print</span><span class="p">(</span><span class="s">"Kiwi number </span><span class="se">\(</span><span class="n">i</span><span class="se">)</span><span class="s"> is a </span><span class="se">\(</span><span class="n">kiwi</span><span class="o">.</span><span class="n">name</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>

Another great addition in Swift is the new standard way of interacting with time, which includes three distinct components: clock, instant and duration. These new APIs integrate nicely with concurrent tasks.

<span class="kd">func</span> <span class="nf">announceWinner</span><span class="p">(</span><span class="n">_</span> <span class="nv">winner</span><span class="p">:</span> <span class="kd">some</span> <span class="kt">Contestant</span><span class="p">)</span> <span class="k">async</span> <span class="k">throws</span> <span class="p">{</span>
    <span class="nf">print</span><span class="p">(</span><span class="s">"Building suspense..."</span><span class="p">)</span>

    <span class="k">try</span> <span class="k">await</span> <span class="kt">Task</span><span class="o">.</span><span class="nf">sleep</span><span class="p">(</span>
        <span class="nv">until</span><span class="p">:</span> <span class="o">.</span><span class="n">now</span> <span class="o">+</span> <span class="o">.</span><span class="nf">seconds</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span>
        <span class="nv">tolerance</span><span class="p">:</span> <span class="o">.</span><span class="nf">seconds</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span>
        <span class="nv">clock</span><span class="p">:</span> <span class="o">.</span><span class="n">suspending</span>
    <span class="p">)</span>

    <span class="nf">print</span><span class="p">(</span><span class="s">"The winner is </span><span class="se">\(</span><span class="n">winner</span><span class="o">.</span><span class="n">name</span><span class="se">)</span><span class="s">!"</span><span class="p">)</span>
<span class="p">}</span>

To learn more about the Async Algorithms package and to discover the best practices for using the Swift Clock type to work with values over time, check out the Meet Swift Async Algorithms WWDC session.

Concurrency support in Swift received many other major upgrades this year, such as optimizations for improved performance, actor prioritization and built-in priority-inversion avoidance. Instruments now has a new Swift Concurrency template that helps us to investigate performance issues. The Visualize and optimize Swift concurrency talk discusses common issues we might encounter in our apps and shows how to use Instruments to find and resolve performance problems.

There are so many more great updates that it’s impossible to cover them all. We are getting faster builds in Xcode thanks to parallelization, faster type checking for function signatures with protocols and where clauses, optimizations for run-time protocol conformance checking and more. With reduced standard library size, Swift can run in a variety of environments, and with streamlined toolchain distribution for Linux, many more of us can enjoy using Swift on our favorite platforms.

We are really looking forward to exploring all of these new features further and to using them in our projects.

 

 

Natalia Panferova is an iOS and macOS engineer and co-founder of Nil Coalescing.
Source Swift
Previous What’s The Difference Between a Junior, Mid-Level, and Senior Developer?
Next Top 5 Use Cases For Google Cloud Spot VMs Explained + Best Practices