swift - Ambiguous type inference -
i'm getting compiler error swift 3.0.1 that's got me stumped. error states there's ambiguity in type of computed property can't see how.
i have protocol generic
property root
. protocol has generic constraint root
must subclass of type root
.
class root { } protocol generic { associatedtype roottype: root var root: roottype { } }
i define protocol extension states:
if
generic
subclass ofroot
, returnself
root
property.
so basically: if it's root
, can forward self
.
extension generic self: root { var root: self { return self } }
i have genericwrapper
class subclass of root
, wraps instance of generic
(to perform root
operations generic
proxy).
class genericwrapper<t: generic>: root { var generic: t init(generic: t) { self.generic = generic } }
finally, define specialised
protocol, , extension states:
if
specialised
implementsgeneric
, returngenericwrapper
root
property.
protocol specialised { } extension specialised self: generic { var root: genericwrapper<self> { { return genericwrapper(generic: self) } } }
then when try implement class implements generic
, specialised
, i'm getting error.
class specialisedimplementation: generic, specialised { // errors: // ambiguous inference of associated type 'roottype': 'genericwrapper<specialisedimplementation>' vs. 'specialisedimplementation' // matching requirement 'root' declaration inferred associated type 'genericwrapper<specialisedimplementation>' // matching requirement 'root' declaration inferred associated type 'specialisedimplementation' }
the reason i'm confused because ambiguity states specialisedimplementation
class matches requirement extension generic
when generic: root
, specialisedimplementation
doesn't inherit root
shouldn't surely?
the diagnostic confusing; real problem here it's circular , it's going beyond compiler can handle.
it tries resolve generic
, , finds can't without assuming specialisedimplementation
subclass of root
(which isn't true, it's desperate find way make work). , tries make specialised
work, can if generic
worked, way generic
work make root
.
you want assume true @ same time, it's not smart. it's trying build piecemeal, 1 protocol @ time, , gets confused. open bugreport @ bugs.swift.org.
(but wildly complicated. in particular, i'd work hard rid of root
class if possibly can; mixing protocols , classes , generics recipe lots of confusing compiler problems.)
Comments
Post a Comment