title: Swift: What is the difference between a typealias and an associatedtype with a value in a protocol?
body: <p>In Swift, the following code compiles without issue.</p>
  
  <pre class="lang-swift prettyprint-override"><code>protocol P1 {
      associatedtype T = Int
  }
  
  protocol P2 {
      typealias T = Int
  }
  </code></pre>
  
  <p>To me, these appear to behave almost identically. The only difference I have noticed is that there are additional restrictions on when you can use <code>P1</code> because it has an associated type. In particular, <code>let x: P1</code> is an error while <code>let x: P2</code> is fine. </p>
  
  <p>What is the actual difference between these two protocols? Are they treated differently in compiled code? Lastly, is there ever an advantage to using <code>P1</code> rather than <code>P2</code>?</p>
  
  <p><strong>Edit for clarity:</strong></p>
  
  <p>I know the working difference between associated types and type aliases, so I am surprised that you are even allowed to give an associated type a fixed value. That seems to defeat the entire purpose of an associated type. I am wondering if there is any utility to giving an associated type a fixed value, and I am wondering if these two protocols are different once compiled. </p>