title: Extending Generic Integer Types in Swift
body: <p>So I'm trying to extend Swift's integer types with a few convenient functions that I use a lot, however I'm not clear on which protocols I should be extending.</p>
  
  <p>As an example, let's say I want to implement a function for clamping a value (if it's less than a minimum set it to that, otherwise if it's greater than a maximum then set it to that instead). My first thought was to do something like this:</p>
  
  <pre><code>extension Int {
      func clamp(minimum:Int, maximum:Int) {
          if self &lt; minimum { return minimum }
          if self &gt; maximum { return maximum }
          return self
      }
  }
  </code></pre>
  
  <p>Bit of a simplistic example, but it illustrates the problem; if I want to now call this for a <code>UInt</code> then naturally I can't, so I have to add an equivalent to <code>UInt</code>, but that won't work for a <code>UInt16</code> and so-on.</p>
  
  <p>I thought that I could perhaps extend something higher up the chain, and use generics instead, however protocols such as <code>IntegerType</code> can't seem to be extended.</p>
  
  <p>So, is there somewhere more appropriate that I could put my extension(s)?</p>