0

当涉及到多态性和动态类型时,我刚刚在 swift 的继承处理中遇到了一个奇怪的行为。下面的代码显示了我遇到的问题,基本上是:动态类型被正确识别(由 打印print("type(of: self) = \(classType)")),但是泛型函数testGeneric使用了错误的类型。

class Global {
    static func testGeneric<T: TestSuperClass>(of type: T.Type) {
        print("T.Type = \(T.self)")
    }
}

class TestSuperClass {
    func run() {
        let classType = type(of: self)
        print("type(of: self) = \(classType)")
        Global.testGeneric(of: classType)
    }
}

class TestClass: TestSuperClass {

}

class TestClass2: TestSuperClass {
    override func run() {
        let classType = type(of: self)
        print("type(of: self) = \(classType)")
        Global.testGeneric(of: classType)
    }
}

let testClass = TestClass()
let testClass2 = TestClass2()

testClass.run()
testClass2.run()

打印输出是

type(of: self) = TestClass
T.Type = TestSuperClass
type(of: self) = TestClass2
T.Type = TestClass2

所以基本上在调用testClass.run()时会type(of: self)产生TestClass,这是我所期望的。那么问题是随后立即调用的泛型函数testGeneric不知何故不适用于 type TestClass,而是使用TestSuperClass

我个人期望的是

type(of: self) = TestClass
T.Type = TestClass
type(of: self) = TestClass2
T.Type = TestClass2

即,testGeneric使用类型TestClass而不是TestSuperClass调用时的泛型函数 via testClass.run()

问题:
- 你对此有解释吗?
- 我怎样才能得到我想到的行为?

4

2 回答 2

2

在 Swift 中,编译器希望在编译时知道要“推断”哪个泛型类型。因此,类型系统将绑定到静态类型。没有动态类型推断之类的东西。

因此编译器生成以下内容(见注释):

class TestSuperClass {
    func run() {
        let classType = type(of: self)  // static MetaType TestSuperClass.Type
        print("type(of: self) = \(classType)") // dynamic type: TestClass
        Global.testGeneric(of: classType)  // infer to static type, i.e. testGeneric<TestSuperClass>
    }
}

结果,T.selfTestSuperClass您的情况下,因为这是编译器能够看到的:

static func testGeneric<T: TestSuperClass>(of type: T.Type) {
    print("T.Type = \(T.self)")
}

您可能想要的是以下内容:

static func testGeneric<T: TestSuperClass>(of type: T.Type) {
    print("T.Type = \(type)")
}

在这里,您不打印 的类型T,而是打印参数的(动态)值type,在您的情况下是TestClass

于 2018-12-19T14:45:45.937 回答
1

回答第二个问题:您将无法更改返回数组的动态类型;它将永远是[TestSuperClass]- 尽管它将包含TestClass对象:

class Global {
    static func testGeneric<T: TestSuperClass>(of type: T.Type) {
        print("T.Type = \(T.self)")
    }
    static func returnObjects<T: TestSuperClass>(of theType: T.Type) -> [T] {
        let newObj = theType.init()
        let newObjType = type(of:newObj)
        print("type(of: newObj) = \(newObjType)")
        return [newObj]
    }
}

class TestSuperClass {
    required init() {
        print ("TestSuperClass.init")
    }

    func run() {
        let classType = type(of: self)
        print("type(of: self) = \(classType)")
        Global.testGeneric(of: classType)
        let array = Global.returnObjects(of: classType)
        let arrayType = type(of:array)
        print("type(of: self) = \(arrayType)")

        print (array)
    }
}

class TestClass: TestSuperClass {
    required init() {
        super.init()
        print("TestClass.init")
    }

}

let testClass = TestClass()
testClass.run()

TestSuperClass.init
TestClass.init
type(of: self) = TestClass
T.Type = TestSuperClass
TestSuperClass.init
TestClass.init
type(of: newObj) = TestClass
type(of: self) = Array < TestSuperClass >
[__lldb_expr_21.TestClass]

于 2018-12-19T16:34:19.260 回答