swift - Default implementations for protocols and associated types -
i strange behaviour follow steps of protocol-orientated-programming , default implementations. have protocol, called testprotocol
, following implemenation
protocol testprotocol { init() func test1() func test2() }
and implementation of protocol, called testclass
:
struct testclass : testprotocol { init() {} func test1() { print("test1") } func test2() { print("test2") } }
i want use class in datastructure called datastructure
, can take elements of testprotocol
. wrote protocol default implementation , implementation:
protocol datastructureprotocol { typealias elementtype: testprotocol } extension datastructureprotocol { func dosomething() { elementtype().test1() elementtype().test2() } } struct datastructure<t: testprotocol> : datastructureprotocol { typealias elementtype = t init() {} }
i don't know if code complete garbage. if so, please don't hesitate correct me. funny part is, dosomething
method doesn't call right methods. prints
test2 test1
which isn't right order. seems default implementation maps function calls wrong implementations. have no idea why. appreciated.
thanks, rusty1s
Comments
Post a Comment