Same Type Constraint in Multiple Protocol in Swift
<p>I have to set a constraint for one type to multiple protocol:</p>

<pre><code>public protocol Test: Hashable { }

public protocol Foo {
    func function() -&gt; Test
}

public protocol Bar {
    func function(argument: Test) -&gt; Test
}
</code></pre>

<p>I want to use these protocols in one class:</p>

<pre><code>public class Baz&lt;T: Test&gt; {
    var FooImpl: Foo
    var BarImpl: Bar
}
</code></pre>

<p>and after I could do this :</p>

<pre><code>class TestImpl : Test { }

let baz = Baz()
baz.BarImpl.function(TestImpl())
</code></pre>

<p>Do you have any ideas?</p>

<p>EDIT : concretely I try to implement a genetic algorithm with Generator, Merger, Mutator, and FitnessEvaluator separated so I have:</p>

<pre><code>public protocol Genotype : Hashable {
}

public protocol CandidateGenerator{
    func generateRandomly&lt;T: Genotype&gt;() -&gt; T
}

public protocol CandidateMerger {
    func merge&lt;T: Genotype&gt;(first: T, second: T) -&gt; T
}

public protocol CandidateMutator {
    func mutate&lt;T: Genotype&gt;(candidate: T) -&gt; T
}

public protocol FitnessEvaluator {
    func evaluate&lt;T: Genotype&gt;(genotype: T) -&gt; Double
}

public class GeneticAlgorithm&lt;T: Genotype&gt; {
    private let generator: CandidateGenerator
    private let merger: CandidateMerger
    private let mutator: CandidateMutator
    private let evaluator: FitnessEvaluator
// ...
}

// at use 
class Test : Genotype { ... }

class Generator : CandidateGenerator {
    func generateRandomly&lt;T: Genotype&gt;() -&gt; T {
        let a = Test()
        return a // &lt;- Error here : cannot convert return expression of type 'Test' to return type 'T'
    }
}
</code></pre>

<p>EDIT :</p>

<p>I tried to use the associedtype but I can't constrain it to Genotype, so my protocols could accept various types not implementing my protocol :</p>

<pre><code>protocol CandidateGenerator {
    associatedtype Genottype

    func generateRandomly() -&gt; Genottype
}

class CandidateGenImpl : CandidateGenerator {
    typealias Genottype = Test

    func generateRandomly() -&gt; Test {
        return /* random */ Test()
    }
}

class Other { } // not implement Genotype

class CandidateGenImpl2 : CandidateGenerator {
    typealias Genottype = Other

    func generateRandomly() -&gt; Other {
        return /* random */ Other()
    }
}
</code></pre>

<p>I tried to do this : </p>

<pre><code>associatedType Genottype = Genotype
</code></pre>

<p>but it doesn't work...</p>

<p>hope this helps to understand</p>