可能的错误?我可以创建忽略约束的通用实例

时间:2015-11-19 17:38:38

标签: swift generics generic-constraints

在处理基于节点的框架(包括序列化和反序列化)的过程中,我发现了一个可以创建忽略类型约束的类实例的情况。为什么我可以在ZNumericProcessor<String>内创建ZSum,但ZNumericProcessor被定义为ZNumericProcessor<T: ZNumeric>,其中String不符合ZNumeric?为什么甚至可以调用初始值设定项,尽管ZSum不确定TZNumeric

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

第二:如果我在ZSum的init中没有约束,我怎么能转换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我试图将其合并但是限制较少的子类initalizer将不会被调用(在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)

0 个答案:

没有答案