I get a really strange behaviour as I follow the steps of protocol-orientated-programming and default implementations.
I have a protocol, now called TestProtocol
, with the following implemenation
protocol TestProtocol {
init()
func test1()
func test2()
}
and an implementation of that protocol, now called TestClass
:
struct TestClass : TestProtocol {
init() {}
func test1() { print("test1") }
func test2() { print("test2") }
}
I want to use this class in a datastructure called Datastructure
, which can take elements of a TestProtocol
. so I wrote an protocol with a default implementation and an 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 that code is complete garbage. If so, please don't hesitate to correct me. The funny part is, that the doSomething
method doesn't call the right methods. It prints
test2
test1
which clearly isn't the right order. It seems like the default implementation maps the function calls to the wrong implementations. I have no idea why. Help is much appreciated.
Thanks, Rusty1s