2

在处理基于节点的框架(包括序列化和反序列化)时,我遇到了一个我可以创建一个忽略类型约束的类的实例的情况。为什么我可以创建一个ZNumericProcessor<String>内部ZSum虽然ZNumericProcessor被定义为不符合ZNumericProcessor<T: ZNumeric>哪里?为什么它甚至可以调用初始化程序,尽管不确定是否如此?StringZNumericZSumTZNumeric

通常我会认为我什至不能对 ZSum 的 init 进行约束以使其编译。

第二:如果我在 init 中没有约束ZSum,我怎么能强制转换,T以便我可以创建一个实例,ZNumericProcessor如果我确定它是的话ZNumeric

代码:

//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

protocol ZNumeric {

}
extension Double: ZNumeric {

}

class ZInput<T> {

}

class ZNumericProcessor<T: ZNumeric> {

}

class ZNode {

    let id: String
    var name: String?

    required init?<T>(type: T.Type, id: String) {
        self.id = id
    }

    init(name: String?) {
        id = NSUUID().UUIDString
    }
}

class ZSum: ZNode {

    required init?<T: ZNumeric>(type: T.Type, id: String) {
        super.init(type: type, id: id)

        let p = ZNumericProcessor<T>()
        print(p)

        if !(T.self is ZNumeric.Type) {
            print("could not create ZSum, type is not Numeric")
            return nil
        }
    }

    override init(name: String?) {
        super.init(name: name)
    }
}

class ZFactory {

    var nodes: [String: ZNode.Type] = [:]
    var types: [String: (type: ZNode.Type, id: String) -> ZNode?] = [:]

    func node(node: String, type: String, id: String) -> ZNode? {

        let n = nodes[node]
        if n == nil {
            return nil
        }

        let t = types[type]
        if t == nil {
            return nil
        }

        return t!(type: n!, id: id)
    }

    func registerNode(name: String, type: ZNode.Type) {
        nodes[name] = type
    }

    func registerType(name: String, processor: (type: ZNode.Type, id: String) -> ZNode?) {
        types[name] = processor
    }
}

class Serializer {
    func node() -> (id: String, node: String, type: String)? {
        return nil
    }
}

var t = ZFactory()
t.registerNode("ZSum", type: ZSum.self)
t.registerType("Double", processor: { (type: ZNode.Type, id: String) -> ZNode? in return type.init(type: Double.self, id: id) } )
t.registerType("String", processor: { (type: ZNode.Type, id: String) -> ZNode? in return type.init(type: String.self, id: id) } )
var n = t.node("ZSum", type: "Double", id: "ID1")
var m = t.node("ZSum", type: "String", id: "ID2")

编辑:阅读此内容后:Stackoverflow Thread我尝试合并它,但尽管提供了限制较少的子类初始化器,但不会被调用(在 ZSum 中)。

import UIKit

var str = "Hello, playground"

protocol ZNumeric {

}

protocol ZStringRepresentable {

}

extension Double: ZNumeric {

}

class ZInput<T> {

}

class ZNumericProcessor<T: ZNumeric> {

}

class ZNode {

    let id: String
    var name: String?

    required init?<T>(type: T.Type, id: String) {
        self.id = id
    }

    init(name: String?) {
        id = NSUUID().UUIDString
    }
}

class ZSum: ZNode {

    required init?<T>(type: T.Type, id: String) {
        super.init(type: type, id: id)
        print("called") // Never happens
    }

    required init?<T: ZNumeric>(type: T.Type, id: String) {
        super.init(type: type, id: id)

        if !(T.self is ZNumeric.Type) {
            print("could not create ZSum, type is not Numeric") // This is called with String
            return nil
        }

        let pp = ZNumericProcessor<T>()
    }

    override init(name: String?) {
        super.init(name: name)
    }
}

class ZFactory {

    var nodes: [String: ZNode.Type] = [:]
    var types: [String: (type: ZNode.Type, id: String) -> ZNode?] = [:]

    func node(node: String, type: String, id: String) -> ZNode? {

        let n = nodes[node]
        if n == nil {
            return nil
        }

        let t = types[type]
        if t == nil {
            return nil
        }

        return t!(type: n!, id: id)
    }

    func registerNode(name: String, type: ZNode.Type) {
        nodes[name] = type
    }

    func registerType(name: String, processor: (type: ZNode.Type, id: String) -> ZNode?) {
        types[name] = processor
    }
}

class Serializer {
    func node() -> (id: String, node: String, type: String)? {
        return nil
    }
}

var t = ZFactory()
t.registerNode("ZSum", type: ZSum.self)
t.registerType("Double", processor: { (type: ZNode.Type, id: String) -> ZNode? in return type.init(type: Double.self, id: id) } )
t.registerType("String", processor: { (type: ZNode.Type, id: String) -> ZNode? in return type.init(type: String.self, id: id) } )
var n = t.node("ZSum", type: "Double", id: "ID1")
var m = t.node("ZSum", type: "String", id: "ID2")

编辑 2:更简单的版本,同样的效果,更严格的初始化程序即使使用 String 也会被调用,尽管 String 不是 ZNumeric:

protocol ZNumeric {

}

extension Double: ZNumeric {

}

protocol ZInitializable {
    init<T>(type: T.Type)
}

class ZNode: ZInitializable {
    required init<T>(type: T.Type) {

    }
}

class ZSum: ZNode {
    required init<T>(type: T.Type) {
        super.init(type: type)

    }
    required init<T: ZNumeric>(type: T.Type) {
        super.init(type: type)
        print("ZSum ZNumeric:\(type)") // Is called both with String and Double
    }
}

func create(node: ZNode.Type) {
    let a = node.init(type: Double.self)
    let b = node.init(type: String.self)
}

create(ZSum.self)
4

0 回答 0