You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 04:19:50