We may find situations in which we want a protocol function to return the current type, here the metatype Self
can come in handy. In these situations rather than just returning the protocol type, we likely want to return the type that conforms to the protocol. An associated type can be added to the protocol that defaults to Self
to achieve this. Pretty handy! 🙌
protocol ChatThreadsRobot {
associatedtype ChatThreadsType: ChatThreadsRobot = Self
func tapCreateThread() -> ChatThreadsType
}
extension ConnectionThreadsRobot: ChatThreadsRobot {
func tapCreateThread() -> ConnectionThreadsRobot {
app.buttons["createThreadButton"].tap()
return self
}
}
Thanks for reading and happy coding! 🙏