我不知道怎麼樣,如果有可能,編寫方法來調用它的構造函數的泛型類型從普通已知的基類繼承< T:基地>打造的T某些情況下不採取即與所有的鐘聲明確的工廠函數並通過類型推斷提供口哨聲。如何在swift中編寫泛型工廠方法?
例,在遊樂場工作:
// Let there be classes MyPod and Boomstick with common Base (not important)
class Base : Printable {
let value : String; init(_ value : String) { self.value = "Base." + value }
var description: String { return value }
}
class MyPod : Base {
init(_ value: String) { super.init("MyPod." + value) }
}
class Boomstick : Base {
init(_ value: String) { super.init("Boomstick." + value) }
}
// PROBLEM: do not know how to force call of Boomstick(n) instead of Base(n) in here
func createSome<T : Base>() -> T[] {
var result = Array<T>()
for n in 1...5 {
result += T(toString(n))
}
return result
}
// This seems to be fine.
// I was expecting call of createSome<Boomstick>() { ... result += Boomstick(n) ...
let objs : Boomstick[] = createSome()
// Prints: Base.1, Base.2, ... not much wished Boomstick.1, Boomstick.2, ...
println(objs)
一個顯而易見的解決方案是創建委託給調用者,但似乎笨拙:
func createSome<T>(factory : (Int)->T) { ... }
謝謝。
PS:是不是createSome(分配) - >基礎[]到OBJ文件:火槍[]類型安全違規?
可能重複的[Swift泛型不保留類型](http://stackoverflow.com/questions/26280176/swift-generics-not-preserving-type) – Lee