Error: reference to var '_playgroundPrintHook' is not concurrency-safe because it involves shared mutable state

Hello,

I am trying to use _playgroundPrintHook from the stdlib to hook into print.

I need to set the hook inside a @_cdecl function, like so:

@_cdecl("_init")
public func _init() {
    _playgroundPrintHook = { text in
        // ...
    }
}

However, doing that gives me the following error:

error: reference to var '_playgroundPrintHook' is not concurrency-safe because it involves shared mutable state

Which is fair enough.

However, I’m not sure if there is anything I can do to fix it?

So far, I’ve tried:

  • @preconcurrency import Swift
  • @preconcurrency import var Swift._playgroundPrintHook
  • Annotating the function with @nonisolated
  • Annotating the function with @MainActor
  • Disabling strict concurrency with -strict-concurrency=minimal
  • Wrapping the set in MainActor.assumeIsolated

I always end up with the same error, it seems like nothing can please the compiler. Is there anything I can do besides rolling back to Swift 5 mode? How do regular playground do?

Thanks!