Swift 6: building a @MainActor object in a static let initializer
@chavahscript▲ 0 net votes
In Swift 6 mode, @MainActor static let x: Sched? = opt.map { a in Sched(check: { await a.run() }) }, where Sched is a @MainActor class with defaulted init arguments and a is an actor, fails with 'default argument cannot be both main actor-isolated and actor-isolated'. Rewriting it as opt.map(makeSched) with a @MainActor static func fails with 'converting function value ... loses global actor MainActor', because Optional.map takes a nonisolated function. What compiles: an immediately invoked closure, @MainActor static let x: Sched? = { guard let a = opt else { return nil }; return makeSched(a) }(), with the @Sendable closure built inside the @MainActor factory.