You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can use tasks to break up your program into isolated, concurrent pieces. Tasks are isolated from each other, which is what makes it safe for them to run at the same time, but sometimes you need to share some information between tasks. Actors let you safely share information between concurrent code.
Like classes, actors are reference types, so the comparison of value types and reference types in Classes Are Reference Types applies to actors as well as classes...
actorMyActor{publicletconstValue:Int=9publicvarmyValue:Stringpublicinit(_ myValue:String){self.myValue = myValue
}publicfunc myFunc(){print("myFunc called with \(myValue)...")}}letactorObject=MyActor("Value")await actorObject.myFunc()varvalue=await actorObject.myValue
A few things to keep in mind are:
By default, you cannot export properties in actors with @objc by default, unless they are marked with nonisolated, which means the property is immutable (a let property, or a getter). A workaround may be needed to set and get properties, one of which can be to add a "get" and "set" async function pair for each needed property, since async functions can be exported to Objective-C (as actors are async by default).
@objcpublicactorMyObjCActor{
// This is just an example. In the real implementations, there will be a wrapped value of the original Actor
publicvarmyValue:String@objcpublicfunc getMyValue()async->String{return myValue;
}@objcpublicfunc setMyValue(_ newValue:String){
myValue = newValue;
}
// ...
}
They also do not support exporting functions with @objc by default unless they are marked with nonisolated or the function is asynchronous. This might require similar workarounds, where the wrapper implementation has the function as async.
Actors do not support subscripts in @objc, and since subscripts cannot be async, an async getter function will be needed to replace this, with similar workaround as the previous.
Similar functionality may follow similar async-ing as described.
Other than that, actors can be expressed as CompountDeclarations, as they are represented similar to, if not same as, classes in Objective-C.
This issue also tracks adding support for isolated parameters using Actors. Since they are both very related, they can be fine in the same issue
A function can become actor-isolated by indicating that one of its parameters is isolated. For example, the deposit(amount:) operation can now be expressed as a module-scope function as follows:
The only thing to note is that isolated parameters require the function to be async when exporting to @objc, so the wrapper implementation will need to have the new function as async
The text was updated successfully, but these errors were encountered:
From https://docs.swift.org/swift-book/documentation/the-swift-programming-language/concurrency#Actors:
A few things to keep in mind are:
@objc
by default, unless they are marked withnonisolated
, which means the property is immutable (alet
property, or a getter). A workaround may be needed to set and get properties, one of which can be to add a "get" and "set" async function pair for each needed property, since async functions can be exported to Objective-C (as actors are async by default).@objc
by default unless they are marked withnonisolated
or the function is asynchronous. This might require similar workarounds, where the wrapper implementation has the function asasync
.subscript
s in@objc
, and since subscripts cannot be async, an async getter function will be needed to replace this, with similar workaround as the previous.Similar functionality may follow similar
async
-ing as described.Other than that, actors can be expressed as
CompountDeclaration
s, as they are represented similar to, if not same as, classes in Objective-C.This issue also tracks adding support for
isolated
parameters using Actors. Since they are both very related, they can be fine in the same issueFrom https://github.com/swiftlang/swift-evolution/blob/main/proposals/0313-actor-isolation-control.md:
The only thing to note is that
isolated
parameters require the function to beasync
when exporting to@objc
, so the wrapper implementation will need to have the new function asasync
The text was updated successfully, but these errors were encountered: