title: Swift extends determined generics type without using objc bindings
body: <p>I wanted to try implementing message pack in pure swift. Everything worked well until I came to Array. </p>
  
  <p>The approach I had was to implement a protocol called <code>MsgPackMarshable</code> thats primitiv types extended : </p>
  
  <pre><code> //Protocol
   protocol MsgPackMarshable{
  
      func msgpack_marshal() -&gt; Array&lt;Byte&gt;
  }
  
  //Implementation for some types
  extension UInt8 : MsgPackMarshable{
      func msgpack_marshal() -&gt; Array&lt;Byte&gt;{
  
          return [0xcc, self]
      }
  }
  
  extension UInt16 : MsgPackMarshable{
      func msgpack_marshal() -&gt; Array&lt;Byte&gt;{
  
          let bigEndian: UInt16 = self.bigEndian
          return [0xcd, Byte((bigEndian &amp; 0xFF00) &gt;&gt; 8), Byte(bigEndian &amp; 0x00FF)]
      }
  }
  
  extension UInt32 : MsgPackMarshable{
      func msgpack_marshal() -&gt; Array&lt;Byte&gt;{
  
          let bigEndian: UInt32 = self.bigEndian
          return [0xce, Byte((bigEndian &amp; 0xFF000000) &gt;&gt; 24), Byte((bigEndian &amp; 0xFF0000) &gt;&gt; 16), Byte((bigEndian &amp; 0xFF00) &gt;&gt; 8), Byte(bigEndian &amp; 0x00FF)]
      }
  }
  </code></pre>
  
  <p>I had some troubles extending <code>Array</code>. I wanted to verify dynamically that the type of the array were implementing the <code>MsgPackMarshable</code> protocol:</p>
  
  <pre><code>extension Array: MsgPackMarshable{
      func msgpack_marshal() -&gt; Array&lt;Byte&gt;{
  
          for item in self{
  
             //How to check type here?
          }
                      return []
      }
  }
  </code></pre>
  
  <p>Due to the fact that <code>Array</code> is a struct in swift, I wanted to avoid redefining a new type of <code>Array&lt;MsgPackMarshable&gt;</code> that embeds an Array. </p>