Swift 1.2b3: Constraining parent type parameters in method has no effect
| Originator: | rix.rob | ||
| Number: | rdar://20167514 | Date Originated: | 15-Mar-2015 05:24 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode-beta (6D543q) |
| Classification: | Other Bug | Reproducible: | Always |
Summary:
A struct/class/enum can have type parameters, and its methods can constrain those type parameters, but the constraints are ignored.
Steps to Reproduce:
1. Compile this:
struct S<T> {
func f<U where T: Hashable>() -> Set<T> {
return Set()
}
}
Expected Results:
I expected this to compile. Alternatively, it would be nice to have a helpful error message explaining why this isn’t supported, even if it’s just “methods cannot constrain the receiving type’s parameters because that’s unimplemented.”
Actual Results:
./boom.swift:4:50: error: type 'T' does not conform to protocol 'Hashable'
Regression:
N/A
Notes:
Note that it makes no difference whether you actually use the U type parameter at all.
This would be absurdly useful to support, especially if you could do so without needing a dummy type parameter, e.g.:
struct S<T> {
func f<where T: Hashable>() -> Set<T>
}
I’m sure it’s complicated how this would interact with protocol requirements (e.g. if f is required by a conformed-to protocol, can it add extra constraints?), but no less useful for it. As it is you have to use free functions for everything like this.
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!