Possible misbehavior of Swift generics when type is constrained to AnyObject
| Originator: | garth | ||
| Number: | rdar://22017417 | Date Originated: | 27-Jul-2015 02:06 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode 7 beta 4 |
| Classification: | Other Bug | Reproducible: | Always |
Please take a look at this code, which produces the error shown:
func test(x: AnyObject) {
print("Test OK")
}
struct Wrapper<T: AnyObject> {
init (_ aValue: T) {
print("Wrapper OK")
}
}
protocol Foo: class {}
class Bar: Foo {}
let p: Foo = Bar()
p is AnyObject // -> true
test(p) // Fine
let d = Wrapper(p) // Cannot invoke initializer for type 'Wrapper<T>' with an argument list of type '(Foo)'
I’m not 100% sure that this is an error, but the result does seem inconsistent. If a Foo is a valid argument for the test() function, I would have expected it to be a valid argument for Wrapper.init().
Here, the compiler seems to want the Foo protocol to be marked as @objc in order to make Foo-conformant objects be legitimate arguments to Wrapper.init().
Just because it has some superficial similarity, let me note that this is probably NOT the “protocols don’t conform to themselves” issue that has been coming up a lot in the context of Swift 2. A Foo-conformant object is in fact AnyObject-conformant, as shown above.
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!