考虑以下游乐场:
import Foundation protocol StringInitable { init( string:String ) } class A : StringInitable { var stored:String required init ( string:String ) { stored = string } } class B : A /*,StringInitable */ { var another_stored:String required init ( string:String ) { another_stored = "B-store" super.init(string: string) } } func maker<T:StringInitable>(string:String) -> T { return T(string: string) } let instanceA = A(string: "test-maker-A") let instanceB = B(string: "test-maker-B") let makerA:A = maker("test-maker-A") let makerB:B = maker("test-maker-B") let typeInstanceA = _stdlib_getTypeName(instanceA) let typeMakerA = _stdlib_getTypeName(makerA) let typeInstanceB = _stdlib_getTypeName(instanceB) let typeMakerB = _stdlib_getTypeName(makerB)
从结果来看,编译器似乎推断出了正确的类型,但未能调用正确的初始化器.为什么我必须在B类中显式实现StringInitable(通过删除B类定义中的注释来测试)以使通用函数“maker”调用正确的初始化器?