Async properties and Actor protocol conformance

With Actor isolated properties being async access from outside of the Actor, should it not be possible to conform to a protocol that has a property getter specified as async?

If I try the following, I get the unexpected error Actor-isolated property 'someAsyncInteger' cannot be used to satisfy a protocol requirement.

protocol AsyncPropertyProtocol {
    var someAsyncInteger: Int { get async }
}

actor ActorInstance: AsyncPropertyProtocol {
    var someAsyncInteger: Int = 0
}

The same goes for a computed property like so:

protocol AsyncPropertyProtocol {
    var someAsyncInteger: Int { get async }
}

actor ActorInstance: AsyncPropertyProtocol {
    var someAsyncInteger: Int {
        return 0
    }
}

Is this something that should work, still has to be implemented or am I doing something wrong?

Yes, it is possible. Why didn't you try it?

protocol AsyncPropertyProtocol {
    var someAsyncInteger: Int { get async }
}

actor ActorInstance: AsyncPropertyProtocol {
    var someAsyncInteger: Int {
        get async {  // <-- Note the getter is marked as 'async'.
            return 0
        }
    }
}

Ok yes, I should have left out a computed property, this does not work for a stored one. I guess I could make a computed property access the stored one, but I would say this has to work directly.

Although even with the computed property, if I would want to make the property synchronous from within the actor, I don’t want to make the getter async.

This looks like a bug to me. The protocol conformance section of SE-0306 says that async requirements can be satisfied by actor-isolated declarations. The protocol conformance section of SE-0310) says that the property declaration can have fewer effects than the requirement in the protocol (but doesn’t explicitly show actor in its examples). The review periods for these proposals overlapped, so I can see how this might have been missed. Maybe @Douglas_Gregor can comment?

Terms of Service

Privacy Policy

Cookie Policy