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-1176] Using protocols (inheriting from class) as generic parameter which needs to be of type AnyObject fails #43784
Comments
Comment by Peter Livesey (JIRA) Why was this marked as a duplicate of SR-55? It seems like a different issue? It at least gives a different error message. |
It's the same underlying problem in the implementation. |
s/problem/limitation to be more precise... |
Comment by Peter Livesey (JIRA) Great. thanks for the fix |
@slavapestov This was marked as "Done". However the other duplicates of SR-55 were resolved with "Duplicate". Was this intentional, or should the resolution for this issue also be "Duplicate"? |
Yeah it's a duplicate. |
Comment by Łukasz (JIRA) @slavapestov Is that fixed? I'm facing the same problem right now in Xcode 9.1 beta 2, Swift version 4.0.1 (swiftlang-900.0.68 clang-900.0.38) public protocol WeakContainerType {
associatedtype ObjectType: AnyObject
weak var storedObject: ObjectType? { get }
}
public struct WeakContainer<T: AnyObject>: WeakContainerType {
public typealias ObjectType = T
public private(set) weak var storedObject: ObjectType?
public init(_ object: ObjectType) {
storedObject = object
}
}
protocol MyDelegate: class {
func delegateMethod()
}
func test(myDelegate: MyDelegate) {
let weakProtocolInstance = WeakContainer<MyDelegate>(myDelegate)
}
/*
error: AnyObjectProtocolPlayground.playground:14:32: error: cannot invoke initializer for type 'WeakContainer<MyDelegate>' with an argument list of type '(MyDelegate)'
let weakProtocolInstance = WeakContainer<MyDelegate>(myDelegate)
^
*/
|
Comment by Łukasz (JIRA) I found more details related to this bug here https://bugs.swift.org/browse/SR-6039 probably |
Yes, this is a duplicate of SR-55. |
Attachment: Download
Environment
Xcode Version 7.3 (7D175)
OS X Version 10.11.4 (15E65)
Additional Detail from JIRA
md5: 10b769def9e1e1f271c406d677bd5b09
duplicates:
Issue Description:
When using a generic struct (
WeakContainer<T>
) where the generic type needs to inherit fromAnyObject
, it can't be created by passing a protocol (declared by inheriting fromclass
) for the generic typeT
, although no other object than one that inherits fromAnyObject
can implement said protocol.Steps to Reproduce:
struct WeakContainer<T: AnyObject>
) with a generic type that needs to inherit fromAnyObject
MyDelegate: class
).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 implementAnyObject
.Actual Results:
Instead Xcode shows an error stating that the protocol can't be used as a concrete type conforming to
AnyObject
. (e.g.: UsingMyDelegate
as concrete type conforming to protocolAnyObject
is not supported)Full source code (also attached as playground):
The text was updated successfully, but these errors were encountered: