Protocol does not explicitly support generics.
| Originator: | Nikita.Leonov | ||
| Number: | rdar://23345228 | Date Originated: | October, 31 2015 |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode 7.1 |
| Classification: | Serious Bug | Reproducible: | 100% |
Summary:
There is no generic support for protocols, only type aliases. It is possible to achieve the same goal as with generics with protocol but it is require significant overhead in code. Here is an example how to define generic protocol:
struct Container<T> {
private var containedValue: T
init(_ value: T) {
containedValue = value
}
}
protocol ContainerProtocol {
typealias ContainerType
var value: ContainerType { get set }
}
extension Container: ContainerProtocol {
typealias ContainerType = T
var value: ContainerType {
get {
return self.containedValue
}
set {
self.containedValue = newValue
}
}
}
class App<T: ContainerProtocol where T.ContainerType == Int> {
var container: T
init(_ container: T) {
self.container = container
}
}
as shown App variable container has variable container: ContainerProtocol<Int> however it is impossible to define it with shorter with a familiar generics syntax.
Steps to Reproduce:
Try to declare protocol with generic at the end like following:
protocol GenericProtocol<T> {
}
Expected Results:
It should work.
Actual Results:
It will not work.
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!