Using protocols (inheriting from class) as generic parameter which needs to be of type AnyObject fails
| Originator: | mdf.friedrich | ||
| Number: | rdar://24975479 | Date Originated: | |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | |
| Classification: | Serious Bug | Reproducible: | Always |
Summary: When using a generic struct (WeakContainer<T>) where the generic type needs to inherit from AnyObject, it can't be created by passing a protocol (declared with inheriting from `class`) for the generic type T, although no other object than one that inherits from AnyObject can implement said protocol. Steps to Reproduce: 1. Create a generic type (e.g. struct WeakContainer<T: AnyObject>) with a generic type that needs to inherit from AnyObject 2. Create a protocol which inherits from class (e.g. MyDelegate: class). 3. Try to declare a variable of the generic type passing the protocol as generic parameter (e.g. WeakContainer<MyDelegate>) Expected Results: It should work without any errors since the only objects which are able to implement the protocol (e.g. MyDelegate) are classes which also implement AnyObject. Actual Results: Instead Xcode shows an error stating that the protocol can't be used as a concrete type conforming to AnyObject. (e.g.: Using 'MyDelegate' as concrete type conforming to protocol 'AnyObject' is not supported) Version: Xcode Version 7.2.1 (7C1002) OS X Version 10.11.3 (15D21) Notes: A playground has been attached showing the problem. Configuration:
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!
Playground
Playground can be downloaded from here: https://dl.dropboxusercontent.com/u/20220723/AnyObjectProtocolPlayground.playground.zip