<div dir="ltr">Oh, I forgot the signed IntN types:<div><br><div><div>extension Int8 : BitPatternRepresentable {</div><div>    var bitPattern: UInt8 { return UInt8(bitPattern: self) }</div><div>    init(bitPattern: UInt8) { self = Int8(bitPattern: bitPattern) }</div><div>}</div><div>extension Int16 : BitPatternRepresentable {</div><div>    var bitPattern: UInt16 { return UInt16(bitPattern: self) }</div><div>    init(bitPattern: UInt16) { self = Int16(bitPattern: bitPattern) }</div><div>}</div><div>extension Int32 : BitPatternRepresentable {</div><div>    var bitPattern: UInt32 { return UInt32(bitPattern: self) }</div><div>    init(bitPattern: UInt32) { self = Int32(bitPattern: bitPattern) }</div><div>}</div><div>extension Int64 : BitPatternRepresentable {</div><div>    var bitPattern: UInt64 { return UInt64(bitPattern: self) }</div><div>    init(bitPattern: UInt64) { self = Int64(bitPattern: bitPattern) }</div><div>}</div></div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jul 11, 2017 at 1:57 PM, Jens Persson <span dir="ltr">&lt;<a href="mailto:jens@bitcycle.com" target="_blank">jens@bitcycle.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>I&#39;ve found it practical/necessary to write my own BitPatternRepresentable protocol and IMHO it feels like something that could have been added along with the improved numeric protocols of Swift 4.</div><div><br></div><div>Would it make sense to add something like the following to the standard library?</div><div><br></div><div><div>/// A type that can be converted to and from an associated BitPattern type.</div><div>protocol BitPatternRepresentable {</div><div>    associatedtype BitPattern<br></div><div>    var bitPattern: BitPattern { get }<br></div><div>    init(bitPattern: BitPattern)</div><div>}</div></div><div><br></div><div><div>I think it&#39;s preferable to keep the conforming type&#39;s BitPatterns to the most basic (most &quot;BitPattern-like&quot; types) so eg</div><div>UInt8, UInt16, UInt32, UInt64</div><div>rather than</div><div>Int8, Int16, Int32, Int64</div><div>and, depending on platform</div><div>UInt64, UInt32</div><div>rather than</div><div>Int or UInt.</div></div><div><br></div><div>PS</div><div><br></div><div>// Double and Float already fulfill the requirements of BitPatternRepresentable so:<br></div><div><br></div><div><div>extension Double : BitPatternRepresentable {}</div><div>extension Float : BitPatternRepresentable {}</div></div><div><br></div><div>// And here is the rest of the types that I&#39;ve used:</div><div><br></div><div><div>extension UInt8 : BitPatternRepresentable {</div><div>    var bitPattern: UInt8 { return self }</div><div>    init(bitPattern: UInt8) { self = bitPattern }</div><div>}</div><div>extension UInt16 : BitPatternRepresentable {</div><div>    var bitPattern: UInt16 { return self }</div><div>    init(bitPattern: UInt16) { self = bitPattern }</div><div>}</div><div>extension UInt32 : BitPatternRepresentable {</div><div>    var bitPattern: UInt32 { return self }</div><div>    init(bitPattern: UInt32) { self = bitPattern }</div><div>}</div><div>extension UInt64 : BitPatternRepresentable {</div><div>    var bitPattern: UInt64 { return self }</div><div>    init(bitPattern: UInt64) { self = bitPattern }</div><div>}</div><div>#if arch(x86_64) || arch(arm64)</div><div>    extension Int : BitPatternRepresentable {</div><div>        var bitPattern: UInt64 { return UInt64(UInt.init(bitPattern: self)) }</div><div>        init(bitPattern: UInt64) { self = Int(Int64(bitPattern: bitPattern)) }</div><div>    }</div><div>    extension UInt : BitPatternRepresentable {</div><div>        var bitPattern: UInt64 { return UInt64(self) }</div><div>        init(bitPattern: UInt64) { self = UInt(bitPattern) }</div><div>    }</div><div>#elseif arch(i386) || arch(arm)</div><div>    extension Int : BitPatternRepresentable {</div><div>        var bitPattern: UInt32 { return UInt32(UInt.init(bitPattern: self)) }</div><div>        init(bitPattern: UInt32) { self = Int(Int32(bitPattern: bitPattern)) }</div><div>    }</div><div>    extension UInt : BitPatternRepresentable {</div><div>        var bitPattern: UInt32 { return UInt32(self) }</div><div>        init(bitPattern: UInt32) { self = UInt(bitPattern) }</div><div>    }</div><div>#endif</div></div><span class="HOEnZb"><font color="#888888"><div><br></div><div><br></div><div>/Jens</div></font></span></div>
</blockquote></div><br></div>