Possible Bug? I can create generic instance ignori

2019-05-21 03:35发布

While working on a node based framework including serialization and deserialization i stummbeld over a case where i can create an instance of a class ignoring the type constraint. Why can i create a ZNumericProcessor<String> inside ZSum although ZNumericProcessor is definded as ZNumericProcessor<T: ZNumeric> where String does not conform to ZNumeric? And why does it even works that the initializers are called although for ZSum it is not sure that T is ZNumeric?

Normally i woud have thought that i can`t even have the constraint on the init of ZSum to get it to compile.

Second: If i would not have the constraint in the init of ZSum, how could i cast T so that i can create an instance of ZNumericProcessor if i am sure it is ZNumeric?

Code:

//: 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")

Edit: After reading this: Stackoverflow Thread i tried to incorporate it but the less restrictive subclass initalizer will not be called (in ZSum) although provided.

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")

Edit 2: Simpler version, same effect, the more restrictive initializer is called even with String although String is not 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条回答
登录 后发表回答