Is it possible to pass generic protocols into a constructor for proper Dependency Injection in Swift 3?
<p>I am trying to accomplish protocol oriented programming in Swift 3 using generics. Is this not fully supported yet? I'm going to show you what I would LIKE to do below but will not compile. Am I missing something here? My goal is to be able to use protocol oriented programming to perform dependency injection with the intent of easily mocking these structures in my unit tests.</p>

<pre><code>protocol ZombieServiceProtocol {

    func fetchZombies()
    var zombieRepository: RepositoryProtocol&lt;Zombie&gt; { get set }
}

struct ZombieService: ZombieServiceProtocol {

    var zombieRepository: RepositoryProtocol&lt;Zombie&gt;

    init(zombieRepository: RepositoryProtocol&lt;Zombie&gt;) {
        self.zombieRepository = zombieRepository
    }

    func fetchZombies() {
        self.zombieRepository.deleteAll()
        self.createFakeZombies()
    }

    private func createFakeZombies() {
        for index in 1...100 {
            let zombie = Zombie(id: index, name: "Zombie \(index)")
            self.zombieRepository.insert(zombie)
        }
    }
}
</code></pre>

<p>The Zombie class looks like this:</p>

<pre><code>public struct Zombie: Persistable {

    var id: Int
    let name: String?
    init(id: Int, name: String?) {
            self.id = id
            self.name =name
    }
}
</code></pre>

<p>Its Persistable protocol looks like this:</p>

<pre><code>protocol Persistable {  
    var id: Int { get set }
}
</code></pre>

<p>And my Repository code looks something like this:</p>

<pre><code>protocol RepositoryProtocol: class {
    associatedtype Object: Persistable

    //...

    func insert(_ object: Object) -&gt; Void
    func deleteAll(_ predicate: (Object) throws -&gt; Bool) -&gt; Void
}

class Repository&lt;Object: Persistable&gt;: RepositoryProtocol {

    var items = Array&lt;Object&gt;()

    //...

    func insert(_ object: Object) {
        self.items.append(object)
    }

    func deleteAll() {
        self.items.removeAll()
    }

}
</code></pre>

<p>I get the following error in my ZombieServiceProtocol:</p>

<ul>
<li>Cannot specialize non-generic type 'RepositoryProtocol'</li>
</ul>

<p>I get the following error in my ZombieService:</p>

<ul>
<li>Cannot specialize non-generic type 'RepositoryProtocol'</li>
<li>Member 'insert' cannot be used on value of protocol type
'RepositoryProtocol'; use a generic constraint instead</li>
</ul>

<p>And to highlight exactly what I'm trying to accomplish, here is what a simple test would look like in which I create a Mock repository and attempt to use that instead of the real one in my ZombieService:</p>

<pre><code>@testable import ZombieInjection
class ZombieServiceTests: XCTestCase {

    private var zombieRepository: RepositoryProtocol&lt;Zombie&gt;!
    private var zombieService: ZombieServiceProtocol

    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
        self.zombieRepository = RepositoryMock&lt;Zombie&gt;()
        self.zombieService = ZombieService(zombieRepository: self.zombieRepository)
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    func testExample() {
        // Arrange
        // Act
        self.zombieService.fetchZombies()

        // Assert
        XCTAssert(self.zombieRepository.count() &gt; 0)
    }
}
</code></pre>

<p>This code also does not compile presently with the same errors as above.</p>

<p>I have been looking at the associatedTypes and typeAlias tags as well as the <a href="https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md" rel="noreferrer">Generics Manifesto</a>. While looking at the Manifesto, I believe this falls into the "Generic Protocols" section which is currently marked as Unlikely (which is bumming me out). If I can't accomplish something like I'm trying to do above, what would be the next best solution?</p>