New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-6039] A protocol A: class
is not any AnyObject
#48596
Comments
This is correct behavior. While an array of protocol values can be converted to an array of The reason for the different representation is to store how the instance conforms to the protocol, in addition to the reference itself. (@slavapestov, what do you think we should do with this bug?) |
Comment by Helge Heß (JIRA) To be honest I don't understand anything of what you said. Why does the array matter? Why has converting a String to NSString has any relevance here? This is not about conversion but a type being a reference type. My question is about pure Swift. The protocol declares explicitly that the type represents an object (using `protocol A: class`). It can even explicitly declare that the protocol is an AnyObject (using `protocol A: class, AnyObject`). Yet it still isn't convertible to AnyObject. That just makes zero sense and doesn't sounds like 'correct behaviour' at all. |
A value of protocol type carries a payload together with a witness table. So it can be converted to a reference type via a representation change, but it is not directly usable as one. This is why you cannot bind a protocol type to a generic parameter that is class constrained. This is more of an implementation restriction than a language design decision, but it is difficult to change now so we are probably stuck with it. |
Comment by Helge Heß (JIRA) OK, so I suppose that means it is more for Swift 5 then. Fair enough. |
Comment by Łukasz (JIRA) But why is that marked as resolved? |
I resolved it because it's a duplicate of SR-55. |
Fun fact - if you declare a protocol like this:
and cmd click the "class" word in Xcode (9.2), it will navigate to:
So even Xcode thinks they are the same thing. |
Comment by Alejandro Ravasio (JIRA) @slavapestov said Are we stuck with it indefinitely or is this something we can expect from Swift5 or something in the future? We can already do this using NSObjects in Swift, but it feels more like a smelly hack than an actual solution. |
Environment
Xcode 9 release, fails in both Swift 3.2 and 4.0.
Additional Detail from JIRA
md5: 682bea115f67f555845ccdf4074ae4fd
duplicates:
Issue Description:
I want to restrict a function to only work w/ reference types. In protocols this is expressed using `protocol A : class` and in generics as `T: AnyObject`. So I assumed this would work:
Why is a `protocol A: class` not an `AnyObject`? It should be exactly the same?
But OK, so lets assume `AnyObject` is something different. And indeed I can do this:
But this still results in the same issue.
I'm pretty sure this is a bug or oversight, don't know :-)
P.S. My real code goes more like this, but the above is the easier version:
Which produces
// error: 'MyCoolClassProtocolType' is not convertible to 'AnyObject'
The text was updated successfully, but these errors were encountered: