Actor 'self' can only be passed 'inout' from an async initializer

Context

Let say we have an actor, which have a private struct.
We want this actor to be reactive, and to give access to a publisher that publishes a specific field of the private struct.

The following code seems to work. But I do have questions.

public actor MyActor {
	
	private struct MyStruct {
		var publicField: String
	}
	
	@Published
	private var myStruct: MyStruct?
	
	/* We force a non isolated so the property is still accessible from other contexts w/o await in other modules. */
	public nonisolated let publicFieldPublisher: AnyPublisher<String?, Never>
	
	init() {
		self.publicFieldPublisher = _myStruct.projectedValue.map{ $0?.publicField }.eraseToAnyPublisher()
	}
	
}

Question 1 & 2

In the init, I use _myStruct.projectedValue, which should be strictly equivalent to $myStruct.
Except the latter does not compile. We get the following error:

'self' used in property access '$myStruct' before all stored properties are initialized

Why? AFAICT myStruct should be init’d to nil, so where is the problem?
And why does the former do compile?

Question 3

With _myStruct.projectedValue, I get a warning at compile-time:

Actor 'self' can only be passed 'inout' from an async initializer

What does that mean? Is it possible to get rid of this warning? Is it “dangerous” (can this cause issues later)?

Thanks!

1 Like
Terms of Service

Privacy Policy

Cookie Policy