シリアライゼーションとデシリアライゼーションを含むノードベースのフレームワークで作業しているときに、型制約を無視してクラスのインスタンスを作成できるケースにつまずきました。が準拠していない場所として定義されているのに、なぜZNumericProcessor<String>
内部を作成できるのですか? そして、それが確実ではないにもかかわらず、イニシャライザが呼び出されることさえ機能するのはなぜですか?ZSum
ZNumericProcessor
ZNumericProcessor<T: ZNumeric>
String
ZNumeric
ZSum
T
ZNumeric
通常、ZSum をコンパイルするために、ZSum の init に制約を設定することさえできないと考えていました。
2 番目: の 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:より単純なバージョン、同じ効果、文字列は 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)