title: In Swift, How to extend a protocol with generic type?
body: <p>I want to add a function to all the classes that implement Comparable , such as Int, Float, CGFloat. Here is my code with error reported:</p>
  
  <pre><code>extension Comparable{
      func constraintBetween&lt;T: Comparable&gt;(a:T , b: T) -&gt; T{
          if self &lt; a {
              return a
          }else if self &gt; b{
              return b
          }else{
              return self
          }
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/sgMnb.png" rel="nofollow"><img src="https://i.stack.imgur.com/sgMnb.png" alt="enter image description here"></a>
  Anyone can help to make it right? Thanks in advance!</p>