WWDC22: Use The 'any' Keyword For Any Type That Conforms To a Protocol With An Associated Type
Daily Coding Tip 103
Celebrating WWDC 2022 and the 100th Daily Coding Tip 🥳
All tutorials will be free this week!
We’re all very familiar with the any keyword, but how does it differ from the some keyword? One big difference in Swift 5.7 is that any can now be used for protocols with associated types, which was previously impossible. In order to test the limitations of this, I created a protocol called MyProtocol that has an associated type.
I created two types which are virtually identical: MyConformingType and MyOtherConformingType.
I have another structure called MyType, which uses both the any and some keywords to create instances of MyConformingType. In the SwiftUI view their types are displayed, and a Button offers the ability to change their type.
This is where the difference between the two keywords becomes clear, as it is impossible to change the someExample property.
If you stop on a breakpoint in this function, you may see that someExample is of type MyProtocol.
We’ve ended up with an abstract type which only has the properties required by the protocol.
I tried setting someExample to exactly the same instance of MyConformingType, and even this isn’t considered to be the same type!



