What’s New in Swift

The swift version I will be talking about is 5.9. As every year, Apple continues to develop the Swift language this year. And Swift is stronger and more performant this year, just like every year. Below I will talk about the striking features of the new version of Swift with examples. Let’s start.

if and switch Expressions (SE-0380)

This feature introduces the ability to use if and switch statements as expressions, for the purpose of:

  • Returning values from functions, properties, and closures (either implicit or explicit)
  • Assigning values to variables
  • Declaring variables

if and switch Expressions

if and switch Expressions

For an if or switch to be used as an expression, it would need to meet these criteria:

  • Each branch of the if, or each case of the switch, must be a single expression.
  • Each of those expressions, when type checked independently, must produce the same type.
  • In the case of if statements, the branches must include an else.

Generic improvement

Type inference makes APIs using parameter packs natural to use, without needing to know that the API is using them, i.e call-side might not even aware you are using type parameter pack under the hood.

Generic improvement

Enabling generic abstraction over argument length <each Result> know as type parameter pack (multiple individual type parameters that are “packed” together)

Convenience AsyncStream.makeStream methods (SE-0388)

This feature adds a new static method makeStream on AsyncStream and AsyncThrowingStream that returns both the stream and the continuation, which make the stream’s continuation easier to access.

Convenience AsyncStream.makeStream methods

Custom Actor Executors (SE-0392)

This proposal proposes a way to customize the executors of actors in Swift, which are responsible for scheduling and running tasks on actors. Executors are abstract types that conform to the Executor protocol and provide a enqueue(_:) method to submit tasks to be executed.

Custom Actor Executors

This proposal also defines a SerialExecutor protocol, which is what actors use to guarantee their serial execution of tasks (jobs).

SerialExecutor

All actors implicitly conform to the Actor (or DistributedActor) protocols. Those protocols include the customization point for the executor they are required to run on in form of the the unownedExecutor property.

Add sleep(for:) to Clock (SE-0374)

There is currently a slight imbalance between the sleep APIs for clocks and tasks, and it causes some troubles when dealing with Clock existentials.Add sleep(for:) to Clock

This release introduce a sleep(for:) method that only dealt with durations, and not instants, then we could invoke that API on an existential:

Add sleep(for:) to Clock

C++ Interoperability

We used to implement Swift ↔ Objective-C ↔ C++ to use C++ language. Swift now has direct access to C++. In this way, we will be able to benefit from the power of the C++ language more easily and quickly.

C++ Interoperability

Its use is as simple as shown below!

C++ Interoperability example

Yes, that’s how some standout headers were in Swift. Swift has many more improvements. To see more details, you can watch the session video or read the swift-evoulation document.

To stay updated with upcoming posts about Apple sessions, be sure to subscribe to our newsletter below.

See you in our next article!