generics - Extending Swift's Dictionary to conform to Protocol that requires the use of subscript causes Linker Error -
the title says all, have protocol like:
protocol myprotocol { typealias keytype typealias valuetype subscript(key: keytype) -> valuetype? { set } }
and trying make dictionary
conform myprotocol
like:
extension dictionary: myprotocol { }
results in linker error:
undefined symbols architecture x86_64: "__tfvss10dictionarym9subscriptfq_gsqq0__", referenced from: __ttwuss8hashable___gvss10dictionaryq_q0__18projectname11myprotocols1_fs2_m9subscriptfqqps2_12keytypegsqqs3_15valuetype_ in searcher.o
now, interesting part if reimplement subscript, error goes away, don't know how reimplement without causing infinite recursion.
if instead of { set }
, protocol requires { }
error goes away , extension works desired.
so imagine issue must in set
side of things, trying require { mutating set }
doesn't work.
does know (a) what's going on , (b) how make dictionary
conform myprotocol
, without having resort drastic solutions reimplementing subscript
?
at first convinced bug swift, after probing bit, not sure.
i'm using swift 1.2
edit: managed make work doing following:
extension dictionary: myprotocol { subscript(key: key) -> value? { { let index = self.indexforkey(key) if let = index { return self[i].1 } return nil } set { if let value = newvalue { self.updatevalue(value, forkey: key) } else { self.removevalueforkey(key) } } } }
but re-implementing dictionary's subscript doesn't seem idea me.
edit 2: clearer question.
edit 3: tried same code out in swift 2.0 , still doesn't work. curiously linker error different time:
undefined symbols architecture x86_64: "swift.dictionary.subscript.materializeforset : (a) -> b?", referenced from: protocol witness _test.myprotocol.subscript.materializeforset : (a.keytype) -> a.valuetype? in conformance <a, b a: swift.hashable> [a : b] : _test.myprotocol in _test in testfile.o
Comments
Post a Comment