GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Refined actor initialization and deinitialization

Available from Swift 5.10

Paul Hudson      @twostraws

SE-0327 adds some clarifications about how state inside actors is created and destroyed, and also relaxes some rules that were overly restrictive.

This proposal contains a number of small, specific changes to actors. For example, Swift will now automatically make actors with an async initializer move to the actor's executor when all its properties are initialized.

In code, it means the two print() calls shown below might execute on entirely different threads:

actor Actor {
    var name: String

    init(name: String) async {
        print(name)
        self.name = name
        print(name)
    }
}

let actor = await Actor(name: "Margot")

This means the code has a potential suspension directly after the name property is set.

Hacking with Swift is sponsored by try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Other changes in Swift 5.10…

Download all Swift 5.10 changes as a playground Link to Swift 5.10 changes

Browse changes in all Swift versions

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.