Swift泛型方法应该使用重载泛型函数 [英] Swift generic method should use overloaded generic function
问题描述
我无法通过Swift泛型获得所需的效果。我定义了一些泛型函数,但对于特定情况,我想重写它们以提供其他功能。当我从非泛型方法/函数调用函数时,一切正常(当参数类型匹配时使用特定版本,否则使用泛型版本),但是当我从泛型方法/函数调用函数时,它始终使用泛型版本的功能(从来没有具体的版本)。
下面是一个示例游乐场:
func printSomething< T> (something:T){
println(This is using the generic version。)
println(something)
}
func printSomething(string:String) {
println(This is using the specific version。)
println(string)
}
func printSomeMoreThings< T> (something:T){
printSomething(something)
}
class TestClass< T> {
var something:T
init(something:T){
self.something = something
}
func printIt(){
printSomething(self.something)
}
}
printSomething(a)
println()
printSomeMoreThings(b)
let test = TestClass(something:c)
println()
test.printIt()
$ c $ $ b>
这是使用特定的版本。
a
这是使用通用版本。
b
这是使用通用版本。
c
我希望它始终使用特定版本(因为它调用printSomething一直有一个String参数)。有没有办法做到这一点,而不必重载每个方法/函数与特定的字符串版本。特别是对于类的情况,因为我不能为特定类型的T重载类方法吗?解决方案
现在不能(您无法为特定类型的< T>
)重载实例/类方法。
然而,不要使用函数重载,你可以在运行期间检查类型并采取相应的行动:
func printSomething< T>(something:T)
{
如果让somestring =某事物为?字符串
{
println(这是使用特定版本)
println(somestring)
返回
}
println(This is using the generic version。)
println(something)
}
除非您数千次调用此函数,否则对性能的影响应该可以忽略不计。
I'm having trouble getting the desired effect with Swift generics. I defined some generic functions, but for specific cases I would like to override them to provide additional features. When I call the functions from a non generic method/function everything works fine (it uses the specific versions when the argument types match and the generic version otherwise), but when I call the functions from a generic method/function it always uses the generic version of the function (never the specific versions).
Here's an example playground:
func printSomething <T> (something: T) {
println("This is using the generic version.")
println(something)
}
func printSomething(string: String) {
println("This is using the specific version.")
println(string)
}
func printSomeMoreThings <T> (something: T) {
printSomething(something)
}
class TestClass <T> {
var something: T
init(something: T) {
self.something = something
}
func printIt() {
printSomething(self.something)
}
}
printSomething("a")
println()
printSomeMoreThings("b")
let test = TestClass(something: "c")
println()
test.printIt()
This gives the following output:
This is using the specific version.
a
This is using the generic version.
b
This is using the generic version.
c
I would like it to use the specific version all the time (since it's calling printSomething with a String argument all the time). Is there a way to do this without overloading every method/function with a specific String version. Especially for the Class case, because I can't overload the class methods for specific types of T?
This currently cannot be achieved for reasons that you have mentioned yourself (you can't overload instance/class methods for specific types of <T>
).
However, instead of using function overloading, you can check the type during runtime and act accordingly:
func printSomething<T>(something: T)
{
if let somestring = something as? String
{
println("This is using the specific version.")
println(somestring)
return
}
println("This is using the generic version.")
println(something)
}
The impact on performance should be negligible unless you call this function thousands of times.
这篇关于Swift泛型方法应该使用重载泛型函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!