Swift 通用工厂:错误?
Swift Generic Factory: Bug?
调查 Swift 泛型,并偶然发现一些非常奇怪的行为...我应该提交雷达,还是我误解了什么?使用 Swift 1.2 beta 测试。
代码说话最好,简单的实现限制了工厂实例化 BaseClass 和派生 类:
/// Testing factory pattern to instantiate BaseClass and derived
class BaseClassFactory
{
// Always returns an instance of BaseClass, even if the constant or
// variable storing the result is explicitly typed( see test )
class func makeInstance< T : BaseClass >( type: AnyClass ) -> T?
{
return T()
}
// Returns an instance of DerivedClass only if the constant or variable storing
// the result is explicitly typed.
class func makeInstanceContrived< T : BaseClass >( type: AnyClass ) -> T?
{
let instance : T? = T.makeInstance() as? T
return instance
}
// Works fine
class func makeInstanceAndCallBack< T : BaseClass >( handler: ( instance: T? ) -> Void ) -> Void
{
let myInstance : T? = T.makeInstance() as? T
handler( instance: myInstance )
}
}
class BaseClass
{
// Since T() fails...
class func makeInstance()->BaseClass
{
return BaseClass()
}
}
class DerivedClass : BaseClass
{
override class func makeInstance()->BaseClass
{
return DerivedClass()
}
}
测试,带有非常奇怪行为的屏幕截图(尽管有编译器警告,'is' 测试确实通过了):
// Nope
if let instance = BaseClassFactory.makeInstance( DerivedClass.Type )
{
if instance is DerivedClass == false
{
println( "1: Wrong type..." )
}
}
// Nope, even when typing the constant. This seems like very dangerous behaviour...
if let instance : DerivedClass = BaseClassFactory.makeInstance( DerivedClass.Type )
{
if instance is DerivedClass == false
{
//compiler even gives a warning here: " 'is' test is always true "
println( "2: what the???" )
}
}
// Nope
if let contrivedInstance = BaseClassFactory.makeInstanceContrived( DerivedClass.Type )
{
if contrivedInstance is DerivedClass == false
{
println( "3: Wrong type..." )
}
}
// Yes, typing the constant does the trick here
if let contrivedInstance : DerivedClass = BaseClassFactory.makeInstanceContrived( DerivedClass.Type )
{
println( "4: success! type is: \(contrivedInstance )" )
}
// Yes
BaseClassFactory.makeInstanceAndCallBack()
{
( instance: DerivedClass? ) -> Void in
if let callbackInstance = instance
{
println( "5: success! type is: \(callbackInstance )" )
}
}
你在这里有两个问题——一个是不正确的代码,另一个是一个已知的错误(我的雷达作为 18518629 的复制品被关闭,它在 1.2b4 中仍然是开放的)。
首先,在这个构造中:
class func makeInstance< T : BaseClass >( type: AnyClass ) -> T?
{
return T()
}
// then later
BaseClassFactory.makeInstance( DerivedClass.Type )
你的论点没有任何作用。它本质上是毫无意义的,对 T
的类型没有贡献(怎么可能?参数没有引用 T
)。相反,T
的类型将从上下文中选择,即如果您将结果分配给 DerivedClass
变量,T
将是 DerivedClass
。如果您不指定,默认行为是将 T
作为基数 class,它被限制为 BaseClass
。
你的意思大概是这样的:
class func makeInstance< T : BaseClass >( type: T.Type ) -> T?
{
return T()
}
// then later
BaseClassFactory.makeInstance( DerivedClass.self )
这应该可以将 T
设置为您想要的类型。但它仍然无法工作,因为基 class 没有动态调度的初始化程序导致的错误(泛型只有一个运行时实现,它依赖于正确的 init 被多态调用) .
如果您将 required init() { }
添加到 BaseType
,您将获得正确的行为。
调查 Swift 泛型,并偶然发现一些非常奇怪的行为...我应该提交雷达,还是我误解了什么?使用 Swift 1.2 beta 测试。
代码说话最好,简单的实现限制了工厂实例化 BaseClass 和派生 类:
/// Testing factory pattern to instantiate BaseClass and derived
class BaseClassFactory
{
// Always returns an instance of BaseClass, even if the constant or
// variable storing the result is explicitly typed( see test )
class func makeInstance< T : BaseClass >( type: AnyClass ) -> T?
{
return T()
}
// Returns an instance of DerivedClass only if the constant or variable storing
// the result is explicitly typed.
class func makeInstanceContrived< T : BaseClass >( type: AnyClass ) -> T?
{
let instance : T? = T.makeInstance() as? T
return instance
}
// Works fine
class func makeInstanceAndCallBack< T : BaseClass >( handler: ( instance: T? ) -> Void ) -> Void
{
let myInstance : T? = T.makeInstance() as? T
handler( instance: myInstance )
}
}
class BaseClass
{
// Since T() fails...
class func makeInstance()->BaseClass
{
return BaseClass()
}
}
class DerivedClass : BaseClass
{
override class func makeInstance()->BaseClass
{
return DerivedClass()
}
}
测试,带有非常奇怪行为的屏幕截图(尽管有编译器警告,'is' 测试确实通过了):
// Nope
if let instance = BaseClassFactory.makeInstance( DerivedClass.Type )
{
if instance is DerivedClass == false
{
println( "1: Wrong type..." )
}
}
// Nope, even when typing the constant. This seems like very dangerous behaviour...
if let instance : DerivedClass = BaseClassFactory.makeInstance( DerivedClass.Type )
{
if instance is DerivedClass == false
{
//compiler even gives a warning here: " 'is' test is always true "
println( "2: what the???" )
}
}
// Nope
if let contrivedInstance = BaseClassFactory.makeInstanceContrived( DerivedClass.Type )
{
if contrivedInstance is DerivedClass == false
{
println( "3: Wrong type..." )
}
}
// Yes, typing the constant does the trick here
if let contrivedInstance : DerivedClass = BaseClassFactory.makeInstanceContrived( DerivedClass.Type )
{
println( "4: success! type is: \(contrivedInstance )" )
}
// Yes
BaseClassFactory.makeInstanceAndCallBack()
{
( instance: DerivedClass? ) -> Void in
if let callbackInstance = instance
{
println( "5: success! type is: \(callbackInstance )" )
}
}
你在这里有两个问题——一个是不正确的代码,另一个是一个已知的错误(我的雷达作为 18518629 的复制品被关闭,它在 1.2b4 中仍然是开放的)。
首先,在这个构造中:
class func makeInstance< T : BaseClass >( type: AnyClass ) -> T?
{
return T()
}
// then later
BaseClassFactory.makeInstance( DerivedClass.Type )
你的论点没有任何作用。它本质上是毫无意义的,对 T
的类型没有贡献(怎么可能?参数没有引用 T
)。相反,T
的类型将从上下文中选择,即如果您将结果分配给 DerivedClass
变量,T
将是 DerivedClass
。如果您不指定,默认行为是将 T
作为基数 class,它被限制为 BaseClass
。
你的意思大概是这样的:
class func makeInstance< T : BaseClass >( type: T.Type ) -> T?
{
return T()
}
// then later
BaseClassFactory.makeInstance( DerivedClass.self )
这应该可以将 T
设置为您想要的类型。但它仍然无法工作,因为基 class 没有动态调度的初始化程序导致的错误(泛型只有一个运行时实现,它依赖于正确的 init 被多态调用) .
如果您将 required init() { }
添加到 BaseType
,您将获得正确的行为。