I am trying to implement object oriented code using a common protocol. Let's say I have two protocols.
protocol Executable: class { func execute() } protocol Dockable: class { associatedtype T func dock(object: T) }
I implemented a decorator for the executable:
final class DockableExecutable: Executable, Dockable { typealias T = Executable private let decorated: Executable private var docked: Executable? init(_ decorated: Executable) { self.decorated = decorated }
Now I will not use it in this class:
final class MyViewController: UIViewController { init(save: Executable, uiUpdateConnector: Dockable<Executable>) {} }
But this is impossible, because the protocol itself is not general, but only a function. The compiler tells me:
Unable to specialize non-standard type "Dockable"
The idea is to use it like this:
let dockableExecutable = DockableExecutable( SQLUpdateExecutable() ) let controller = MyViewController(save: dockableExecutable, uiUpdateConnector: dockableExecutable)
What is the correct syntax in Swift 3 to make the compiler happy?
UPDATE 1
I made some progress with the following code:
final class MyViewController: UIViewController { init<DOCKABLE: Dockable>(save: Executable, uiUpdateConnector: DOCKABLE) where DOCKABLE.T: Executable {} }
Strange, maybe someone has a better idea? When using the class now I get:
The general DOCKABLE parameter cannot be output.
So my questions have not changed:
What is the correct syntax in Swift 3 to make the compiler happy?
Update 2
It seems impossible to use fast generic (or better: type-related) protocols in protocol-based object-oriented programming.
Therefore, we must wrap them in some kind of container and lose the protocol-based approach, or we need to define different protocols for each situation.
Since working without protocols is not an option for me, I have to write different protocols without generics. Shame on a quick π