-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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-55] non-@objc protocol existentials do not conform to their own protocol type #42677
Comments
Comment by Chris Willmore (JIRA) Reduced case:
|
Comment by Chris Willmore (JIRA) Doug Gregor and I spoke briefly about this bug and he says making it work is a very deep rabbit hole. |
This was on my agenda – I've discussed it before with Joe Groff and we have a half-plan. Low priority though. |
Comment by nate rivard (JIRA) This is still an issue on Swift 3.2, is there a plan to tackle what seems like a pretty valid use-case (see dupes for more cases?) |
This does compile: @objc protocol P {}
class C: P {}
func process<T: P>(item: T) -> T { return item }
func f(image: P) { let processed: P = process(item:image) } Adding Some of us over on Stack Overflow find this surprising and would like to know whether that's deliberate or a buggy edge-case. |
It's deliberate – lifting this restriction is what this bug is about. Like I said it's tricky and we don't have any concrete plans yet. |
Thank you, @slavapestov ! |
Comment by Ilya Kazakov (JIRA) Anxiously awaiting a positive resolution of this issue by your team. For all its glory the new Codable protocol is quite limited without it. It is going to be a pain to use it, as every class needs to implement its own boilerplate code to support custom mapping. And custom mapping is what we use in the real world to talk to remote systems. |
Comment by Bridger Maxwell (JIRA) I wish this worked so I could make an array of weak listeners (like a delegate, but more than one). This is an example: https://gist.github.com/bridger/f3818fc187a3b3a17b147de31981f06d |
bridger (JIRA User) I've had the same problem (SR-1176). But there's a workaround (not nice but works). I've removed the Quick sample: struct Weak<T> {
private weak var _object: AnyObject?
public var object: T? {
get { return _object as? T }
set { _object = newValue as AnyObject }
}
init?(object: T) {
self._object = object as AnyObject
if object == nil {
return nil
}
}
}
extension Weak where T: AnyObject {
init(object: T) {
self._object = object
}
} Note that the casts to |
I think we should separate out the AnyObject parts of this. AnyObject is special in that you don't need a witness table to conform to it. |
Comment by Marc Palmer (JIRA) @belkadan it would be great if we could get progress on this. It is so painful. I want to have a general purpose I need `T: AnyObject` because my observer set needs observer identity. |
marcpalmer (JIRA User) In Swift 4 everything is convertible to |
Comment by Marc Palmer (JIRA) Thanks @werediver I'll give it a try. However, I don't think you meant what you wrote i.e. not everything is convertible to `AnyObject`: struct A {
let text = "Hello"
}
let value = A()
let x: AnyObject = value
---
error: Test.playground:43:20: error: value of type 'A' does not conform to specified type 'AnyObject'
let x: AnyObject = value
^~~~~
as AnyObject |
marcpalmer (JIRA User) I did mean what I said, but nothing more — I didn't say "implicitly". Everything is convertible to I'd suggest moving this question to Stack Overflow, because it (likely) can be solved in Swift 4 as it is now. |
Comment by Marc Palmer (JIRA) I'm not planning to have a discussion here. The fact is that there is a problem with |
Comment by Marc Palmer (JIRA) Try explaining the nature of this problem to a student who is being taught Swift at school on an iPad. "Here's a protocol... constrain it to AnyObject so that only classes can implement it... now let's show you what generics look like... oh wait." |
Just like a value is not the same thing as an Optional containing that value, neither is a class instance that conforms to a protocol P the same as a value of type |
I'm not 100% sure the AnyObject part belongs in this bug… |
Comment by Maarten Billemont (JIRA) To add a data-point to this (old) bug, while it can be appreciated that there are difficulties involved with allowing protocol types to satisfy protocol constrained generics, we can also appreciate that a language where this is allowed: protocol A {}
protocol B : A {}
struct S1<P> {
var p : P?
}
do {
let a: A? = nil, b: B? = nil, c = "C"
S1(p: a)
S1(p: b)
S1(p: c)
} But wanting to specialize struct S2<P : A> {
var p : P?
}
do {
let a: A? = nil, b: B? = nil, c = "C"
S2(p: a) // unexpected: Protocol 'A' as a type cannot conform to the protocol itself
S2(p: b) // unexpected: Protocol 'B' as a type cannot conform to 'A'
S2(p: c) // expected: Generic struct 'S2' requires that 'String' conform to 'A'
} Is not ideal. This frustrates the ability to create safe specializations, forcing me to go with the "any type goes" ( |
Attachment: Download
Additional Detail from JIRA
md5: 4333a622f7ff4546521a5fc9f3702a7d
is duplicated by:
protocol A: class
is not anyAnyObject
AnyObject
relates to:
Issue Description:
Summary:
The compiler throws the error
at the final line of the following code:
The error is gone when conformance to `MediaProtocol` is removed from the generic type `T` at the `process` method declaration in the given code.
Reproduced on:
Xcode 7.1.1 (7B1005), Swift 2.1, OS X 10.11
The text was updated successfully, but these errors were encountered: