Swift: unable to express type that inherits from a superclass and conforms to a protocol
| Originator: | smileyborg | ||
| Number: | rdar://21343400 | Date Originated: | Jun 11, 2015 |
| Status: | Open | Resolved: | |
| Product: | Swift | Product Version: | Swift 2.0 |
| Classification: | Reproducible: | Always |
Assume we have the following example protocol in Swift:
protocol MyProtocol {
func protocolMethod() -> Void
}
Now, imagine that you want to declare a variable that is of some specific class type (e.g. NSEnumerator), something like:
var foo: NSEnumerator
but in addition to this, you also want to specify that the type of foo must additionally conform to the protocol MyProtocol. (In other words, foo is both an NSEnumerator and also conforms to MyProtocol.)
There is no way to express this in Swift, and this is problematic. Here are some attempts at doing this, which do not work:
var foo: NSEnumerator, MyProtocol // does not compile
var foo: NSEnumerator<MyProtocol> // does not compile
var foo: protocol<NSEnumerator, MyProtocol> // does not compile
Note that this is possible to do in Objective-C, and is fairly common:
NSEnumerator<MyProtocol> *foo;
Steps to Reproduce:
N/A
Expected Results:
N/A
Actual Results:
N/A
Version:
Swift 2.0
Notes:
This Stack Overflow post has some related discussion, but if you look at the comments on this answer it's clear that there isn't a way to declare this in Swift:
http://stackoverflow.com/a/24027473/796419
Configuration:
Xcode 7 beta 1
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!