What is the canonical way of writing a global actor?

Overview

A type that conforms to the GlobalActor protocol and is marked with the @globalActor attribute can be used as a custom attribute. Such types are called global actor types, and can be applied to any declaration to specify that such types are isolated to that global actor type. When using such a declaration from another actor (or from nonisolated code), synchronization is performed through the shared actor instance to ensure mutually-exclusive access to the declaration.

Is the conformance to GlobalActor mandatory?

I ask because these both compile, and seem to work.

Conforming to GlobalActor:

 @globalActor
 actor A: GlobalActor {
     static let shared: A = .init()

     private init () {}
}

Without conforming to GlobalActor:

 @globalActor
 actor A {
     static let shared: A = .init()

     private init () {}
}

What is the difference? :confused:

Simply put: no difference.

Global actors implicitly conform to the GlobalActor protocol.

From language reference.

1 Like