title: How should I write this in Swift?
body: <p>Let's say that I create a protocol like this:</p>
  
  <pre><code>protocol A {
   associatedtype T
   func computeSomething(with:T) -&gt; Double
  }
  </code></pre>
  
  <p>In my generic typed class, I would like to do something like this:  </p>
  
  <pre><code>class B&lt;U&gt; {
   var doSomething:A&lt;U&gt;
  }  
  </code></pre>
  
  <p>This thing is that this generates an error, but I would like to accept any type that would support computeSomething on my type <code>U</code> but I don't know at all how to do that?</p>
  
  <h2>Edit for clarity</h2>
  
  <p>Basically if A was a generic struct or class, that would be possible, but what if no default implementation (provided by class or struct) makes sense here and the only thing I want is to ensure that the type does what I want?</p>
  
  <h2>Edit #2 (with concrete example)</h2>
  
  <p>I wanted to simplify my question which makes it pretty hard to understand so here is a still simplified and fictional problem that probably matches better the issue I am encountering:</p>
  
  <p>I am writing a generic class that processes its generic type <code>T</code>:  </p>
  
  <pre><code>class Process&lt;T&gt; { ... }
  </code></pre>
  
  <p>The class <code>Process</code> itself includes code that processes <code>T</code>, but in order for this code to work, it needs <code>T</code> to conform to some protocols, for instance:  </p>
  
  <pre><code>protocol A {
      func mixWith(other:A) -&gt; A
  }
  
  protocol B {
      var isFoo:Bool { get set }
  }
  </code></pre>
  
  <p>So my first approach was to simply require <code>T</code> to conform to those protocols:  </p>
  
  <pre><code>class Process&lt;T:&lt;A,B&gt;&gt; { ... }
  </code></pre>
  
  <p>This looks like the simplest approach and probably is in many cases, but in my case I think that this actually is problematic, for this reason:</p>
  
  <p>First, I may need to process the same type in many different ways, and changing a way a type is being processed often requires changing the actual implementation of protocols <code>A</code> and <code>B</code> for instance in this case, <code>fooProcess</code> and <code>barProcess</code> are both of type <code>Process</code> with generic type <code>MyType</code>:  </p>
  
  <pre><code>let fooProcess = Process&lt;MyType&gt;()
  let barProcess = Process&lt;MyType&gt;() 
  </code></pre>
  
  <p>But I want <code>fooProcess</code> and <code>barProcess</code> to do different operations which in many cases would require to change the implementation of the <code>A</code> and <code>B</code> protocols of my <code>MyType</code> type and that's simply not possible.</p>
  
  <p>So my first idea was to simply require some closures and variables to be defined so that I wouldn't need protocols anymore and would define the way data is being processed only in my <code>Process</code> class, a little bit like this:  </p>
  
  <pre><code>class Process&lt;T&gt; {
  //
  var mix:(_ lhs:T, _ rhs:T)-&gt;(T)
  var isFoo:(_ a:T)-&gt;(Bool) 
  ...
  }
  </code></pre>
  
  <p>There all of the processing would be directly implemented in my <code>Processing</code> class, again this would have looked like the right solution but now comes another issue, which led me to my associated type approach: it turns out that in many cases, the user of my <code>Process</code> class would want to get some default behaviour implemented by my framework, for instance, I could automatically implement protocol <code>A</code> and <code>B</code> for them as long as their class conformed to protocol <code>X</code>, here is how it did it:  </p>
  
  <pre><code>protocol X:A,B {
  ...
  }
  
  extension protocol X {
     // Here was my default implementation of A and B, which enabled my user to directly get A and B implemented as long as their type conformed to X
  }
  </code></pre>
  
  <p>By using this method, I would let my user directly choose what they wanted to implement themselves, by conforming to protocol <code>X</code> they would only need to write a little bit of code and let my framework to all of the rest by itself, and if they wanted to implement themselves A or B they still could.  </p>
  
  <p><strong>So if I am right, there is no way to do such a thing with my closures implementation.</strong>  </p>
  
  <p>So for this reason, I thought that an associated type protocol would be a good solution because here I could let my users easily get some default behaviour or write their own, so now we are getting back to my original question:  </p>
  
  <pre><code>protocol AProcessing {
      associatedtype U
      func mix(_ lhs:U, _ rhs:U) -&gt; U 
  }
  
  protocol BProcessing {
     associatedtype U
     func isFoo(_ a:U) -&gt; Bool
  }
  </code></pre>
  
  <p>And then do something like that:  </p>
  
  <pre><code>class Process&lt;T&gt; {
  var aProcessor:AProcessing&lt;T&gt;
  var bProcessor:BProcessing&lt;T&gt;
  }
  </code></pre>
  
  <p>Here the advantage compared to closures is that I could write a special class conforming to AProcessing that could provide default implementation, this way:  </p>
  
  <pre><code>class AutomaticAProcessing&lt;T:X&gt;:AProcessing { ... }  
  </code></pre>
  
  <p>That would have enabled my users to so something like that:  </p>
  
  <pre><code>var processData = Process&lt;SomeType&gt;()
  processData.aProcessor = AutomaticAProcessing&lt;SomeType&gt;()
  processData.bProcessor = TheirOwnImplemtation  
  </code></pre>
  
  <p>Not only is this not possible in Swift, but it also feels like I am using too many "hacks" to get things done and there should be an easier language feature to do that, unfortunately I don't know what I should use.</p>