I found that this piece of code generates no errors
protocol Foo{
func foo()
}
let x: Foo? = nil
While the following starts complaining Use of protocol 'Foo' as a type must be written 'any Foo'
protocol Foo: ObservableObject{
func foo()
}
let x: Foo? = nil //Use of protocol 'Foo' as a type must be written 'any Foo'
Since ObservableObject inherits AnyObject so I tried
protocol Foo: AnyObject{
func foo()
}
let x: Foo? = nil
Which again gives no errors.
What exactly is the condition in which writing any when using protocol as type is necessary?
According to the proposal that introduced
any- SE-0335,So it is required for writing existential types of "protocols with
Selfand associated type requirements" since Swift 5.6.ObservableObjectis such a protocol, with anObjectWillChangePublisherassociated type.Note that in Swift 6 and above, or in the Swift 6 language mode, existential types of all protocols need
any.