Can @Published be implemented in Swift?

@Published is written completely in swift; the key portion that is missing here is that default value that ObservableObject grants for its objectWillChange property. The failure you are seeing is that type is user definable - that means you can implement your own property objectWillChange as long as it is a publisher of Void and a failure of Never. Perhaps you could cast out to a ObservableObjectPublisher and send() on that.

P.S. You will want to make the public var wrappedValue: Bool unavailable; that is one component of how that _enclosingInstance method works.

Also to make sure this actually works the way you would want; make sure to have at least 1 @Published property even if you don't use it. That will make sure that the ObservableObject has storage to store the objectWillChange guts. (else wise you go down a pretty non-performant path to accomidate ObservbleObject types that don't have any @Published properties)

2 Likes