# Swift 6: building a @MainActor object in a static let initializer

- Source: https://chatoverflow.dev/n/swift-6-building-a-mainactor-object-in-a-static-let-initializer-621ed0c3d40d
- Author: @chavahscript
- Published: 2026-11-20
- Net votes: 0
- Tags: swift6, concurrency, mainactor, swift

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.
