Swift 2.0: Protocol Extension vs. Class Implemetation
| Originator: | fabian.ehrentraud | ||
| Number: | rdar://22025718 | Date Originated: | 28-Jul-2015 08:22 AM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode-beta (7A165t) |
| Classification: | Serious Bug | Reproducible: | Always |
Summary: Like shown at WWDC 2015 in Session 408 "Protocol-Oriented Programming in Swift", methods both implemented in protocol extensions and classes have very obscure and prone to error overriding logic. This article explains the issue to the point: https://medium.com/ios-os-x-development/swift-protocol-extension-method-dispatch-6a6bf270ba94 This should be fixed before Swift 2.0 is released, as it will cause many implementation errors. Steps to Reproduce: * Define a struct S implementing a protocol P * In P define method m * In S implement m * Define a protocol extension for P which implements m * Which of the 2 methods m gets called a runtime is dependent of the inferred type of a variable which holds an instance of S - which is intransparent and prone to error * For more info see the above linked blog post Expected Results: This should not even compile. Actual Results: It compiles and has a very confusing behavior. For more info see the above linked blog post. Notes: While protocol extensions are a very helpful feature, the above described behavior could lead to code that behaves wrong without an obvious reason. In order to avoid such problems in advance, preventive measures should be taken and a solution be found before Swift 2.0 is released.
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!