Swift: Type not correctly inferred for generic argument that needs to inherit from a generic class
| Originator: | alex | ||
| Number: | rdar://22870249 | Date Originated: | 26-Sep-2015 08:11 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xocde 7 & 7.1 beta 2 |
| Classification: | Crash | Reproducible: | Always |
Summary:
If a generic function parameter T is declared in such a way, that it must inherit from a generic class (e.g. "T: GenericClass<U>"), this information is lost when the variable is used inside the function
Steps to Reproduce:
Open the sample project and try to compile it
Expected Results:
The project should compile fine as it is obvious from the signature of the method OperationChain.chain that O must inherit from ResultOperation<T> and that therefore newOperation is of type ResultOperaiton<T>
Actual Results:
The information that the class of newOperation inherits from ResultOperation<T> is lost and Xcode complains that it "Cannot convert value of type 'O' to expected argument type 'ResultOperation<_>'"
Version:
Xcode 7.0 (7A218) and 7.1 beta (7B75)
Notes:
The issue can be circumvented by explicitly casting newOperation to ResultOperaiton<T>, i.e. by replacing the constructor argument "newOperation" in main.swift:25 by "newOperation as! ResultOperation<T>"
Configuration:
If ResultOperation is not a generic class, the code compiles fine.
Attachments:
'SampleProject.zip' was successfully uploaded.
This contains the following file:
protocol ChainableOperation {
typealias InputType
func setInput(input: InputType)
}
class ResultOperation<ReturnType> {
}
class OperationChain<LastReturnType> {
private var lastOperation: ResultOperation<LastReturnType>
init(_ operation: ResultOperation<LastReturnType>) {
lastOperation = operation;
}
func chain<T, O: ResultOperation<T> where O: ChainableOperation>(newOperation: O) -> OperationChain<T> {
let newChainItem = OperationChain<T>(newOperation as! ResultOperation<T>)
return newChainItem
}
}
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!