【问题标题】:Swift Protocol Inheritance & Generic functionsSwift 协议继承和泛型函数
【发布时间】:2014-10-06 16:26:57
【问题描述】:

考虑以下游乐场:

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”调用正确的初始化程序?

【问题讨论】:

  • 一旦我在 init 函数前面添加了“必需”说明符,它就对我有用。

标签: inheritance swift protocols swift-playground


【解决方案1】:

这听起来像是一个编译器错误,原因很简单:makerB 是一个 B 类型的变量,但它被分配了一个 A 的实例。这应该是不可能的,事实上,如果您尝试打印并且更一般地访问makerB 变量的another_stored 属性,则会引发运行时异常,我不会期待别的。

这是因为如果BA 的子类,则不能将A 的实例分配给B 类型的变量(反之亦然)。

虽然可以将A 类型的变量分配给B 类型的变量,但仅在以下条件下:

  • AB 的显式向下转换已完成(否则编译器会出错)
  • A 变量引用的实例实际上是B 的一个实例(否则应引发运行时异常)

请注意,编译器不仅未能调用正确的初始化程序 - 它还调用了另一个类的初始化程序

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多