0
初期化可能な型を返す関数が必要です(特定の引数などで特定の方法で可能性があります)。同じ結果を得ることは他の多くの方法で可能ですが、私はこの種の構文的砂糖を特に探しています。Swift関数の初期化可能な戻り値型を持っています
protocol P {
init()
}
extension Int: P {
public init() {
self.init()
}
}
// same extension for String and Double
func Object<T: P>(forType type: String) -> T.Type? {
switch type {
case "string":
return String.self as? T.Type
case "int":
return Int.self as? T.Type
case "double":
return Double.self as? T.Type
default:
return nil
}
}
let typedValue = Object(forType: "int")()