Actor遵循含async函数要求的Protocol时的Sendable警告疑问
Swift并发检查下Actor遵循带async函数的协议时的警告疑问
先看我定义的非Sendable类和协议代码:
class NonSendable {} protocol P1 { func doSomething(_ nonSendable: NonSendable) async }
开启Swift并发检查后,用Actor遵循该协议时会收到一条警告:
actor P1Actor: P1 { // 警告:Non-sendable type 'NonSendable?' returned by actor-isolated instance method 'doSomething' satisfying protocol requirement cannot cross actor boundary func doSomething(_ nonSendable: NonSendable) {} }
但如果把Actor改为struct或者class,警告就会消失:
struct P1Struct: P1 { // 无警告 func doSomething(_ nonSendable: NonSendable) async {} }
我无法理解为何只有Actor会出现该警告。既然doSomething在所有场景下都会从async上下文触发,那被actor隔离的函数的参数为什么还需要满足Sendable要求?
内容的提问来源于stack exchange,提问作者user2037296
相关产品推荐
相关产品推荐

