New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-1416] constraints overloaded generic functions fail to specify inside another generic function #44025
Comments
This is correct behavior. Overload resolution is done at compile-time, not run-time. |
Comment by Ling Guo (JIRA) I can't agree. C++'s generic is done at compile-time, yet it can specify correctly. Why swift can't? C++: class AClass {
};
template <typename T>
void foo(T obj) {
NSLog(@"calling generic function");
}
template <>
void foo(AClass obj) {
NSLog(@"calling specific function");
}
template <typename T>
void test(T obj) {
foo(obj);
}
auto obj = AClass();
// calling specific function
foo(obj);
// calling specific function
test(obj); |
Comment by Ling Guo (JIRA) Also, calling foo inside another generic function, which loses no type-information, is clearly can be resolve at compile-time. From my point of view, calling foo directly makes no difference with calling it inside a generic function, for both have full type-information, at compile-time. |
Let me put it a different way: Swift's generics use run-time dispatch (because you're going through a protocol or base class), but overload resolution uses compile-time dispatch. In C++, both of those are compile-time decisions, because templates use an instantiation model, where you get a different copy of the code for every argument you call it with. Swift, by contrast, passes a "witness table" at run time, which describes how to perform any of the operations in the protocol. Since there's only one copy of the code, it can only call one If you want |
Comment by Ling Guo (JIRA) So only one copy of generic function exists, that explains a lot. Thanks for the inside point of view. |
Comment by Ling Guo (JIRA) So I tried it by protocol. I got unexpected result. Did I miss something here? indirect enum AEnum : CustomDebugStringConvertible {
case BasicType(String)
case ContainerType(AEnum)
var debugDescription: String {
switch self {
case let .BasicType(str):
return str
case let .ContainerType(recusion):
return "[\(recusion)]"
}
}
}
protocol Aprotocol {
static func getEnum() -> AEnum
}
extension Aprotocol {
static func getEnum() -> AEnum {
return AEnum.BasicType("\(self.self)")
}
}
extension Int : Aprotocol {
}
extension Array : Aprotocol {
}
extension Array where Element : Aprotocol {
static func getEnum() -> AEnum {
return AEnum.ContainerType(Element.getEnum())
}
}
func testTypeRecusion() {
print(Int.getEnum()) // Int
print([Int].getEnum()) // [Int]
print([[Int]].getEnum()) // [Array<Int>], expected [[Int]]
} |
It's the same issue. Array has two methods called
|
Additional Detail from JIRA
md5: f332a23936885f2cb54f1255fcfe38cf
Issue Description:
Consider this as a bug:
The text was updated successfully, but these errors were encountered: