Allow a subclass to drop a generic parameter by specifying the type parameter in the declaration
| Originator: | tom | ||
| Number: | rdar://19811495 | Date Originated: | 12-Feb-2015 03:07 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Swift 1.2 |
| Classification: | Enhancement | Reproducible: | Always |
Summary:
Currently (Swift 1.2), this class hierarchy cannot be defined:
class Sender<TItem> {
func send(item: TItem) {}
}
class Tweet {}
class TweetSender : Sender<Tweet> {
override func send(item: Tweet) {}
}
Expected Results:
It's possible to declare this kind of class hierarchies, where the TweetSender class can be used without its generic type parameter. For all usages of the specific type parameter, the subclass would have to use the specified type parameter in the declaration (see the 'send' function in the example above).
This would allow me to use the TweetSender class directly:
func xxx() {
var x = TweetSender()
x.send(Tweet())
}
Actual Results:
The compiler does not allow the TweetSender class (or any subclass of a generic class) to drop the generic type parameter by specifying it in the baseclass when declaring the class. The class needs another generic parameter, even if it is not used.
Workaround:
Add a useless type parameter to the derived class to satisfy the compiler:
class Sender<TItem> {
func send(item: TItem) {
}
}
class Tweet {}
class TweetSender<TUseless> : Sender<Tweet> {
override func send(item: Tweet) {
}
}
causing you to still have to specify the type parameter even though it's not used anywhere:
func xxx() {
var x = TweetSender<Tweet>()
x.send(Tweet())
}
The extra <Tweet> generic param is useless here and can only lead to confusion.
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!