<div dir="ltr"><div>I need to define some datatypes differently depending on if the target is 32- or 64-bit.</div><div>The code should compile on any 32- or 64-bit platform.</div><div>Is the following the recommended way to do this?<br></div><div><br></div><div>#if arch(x86_64) || arch(arm64)</div><div>    // Define the types, knowing that we&#39;re on a 64-bit platform.</div><div>#elseif arch(i386) || arch(arm)</div><div>    // Define the types, knowing that we&#39;re on a 32-bit platform.</div><div>#else</div><div>    // Is ending up here possible?</div><div>#endif</div><div><br></div><div><br></div><div><br></div><div>PS</div><div>I might as well throw this somewhat related question in here:</div><div><br></div><div>What would be a good name for a protocol that is adopted *only* by these four most primitive/basic types:<br></div><div><div>UInt8, UInt16, UInt32, UInt64</div><div>?</div><div>The protocol is called PrimitiveBitPatternProtocol in the code below and its purpose is something like this:<br></div><div>Every type that can be represented as a sequence of 8, 16, 32 or 64 bits can be formally associated with one of the above four PrimitiveBitPatternProtocol-conforming types, so for example the Double type conforms to PrimitiveBitPatternRepresentable by having UInt64 as its associated &quot;PrimitiveBitPattern&quot; type.</div></div><div><br></div><div>Or perhaps less confusingly formulated: I would like the following code (which compiles in Swift 4) to have shorter and better names:</div><div><br></div><div><br></div><div><div>protocol PrimitiveBitPatternProtocol : UnsignedInteger, FixedWidthInteger {}</div><div>extension UInt8 : PrimitiveBitPatternProtocol {}</div><div>extension UInt16 : PrimitiveBitPatternProtocol {}</div><div>extension UInt32 : PrimitiveBitPatternProtocol {}</div><div>extension UInt64 : PrimitiveBitPatternProtocol {}</div><div><br></div><div>protocol PrimitiveBitPatternRepresentable {<br></div><div>    associatedtype PrimitiveBitPattern: PrimitiveBitPatternProtocol</div><div>    init(bitPattern: PrimitiveBitPattern)</div><div>    var bitPattern: PrimitiveBitPattern { get }</div><div>}</div><div><br></div><div>// Double and Float already satisfies the requirements of</div><div>// PrimitiveBitPatternRepresentable so:</div><div>extension Double : PrimitiveBitPatternRepresentable {}</div><div>extension Float : PrimitiveBitPatternRepresentable {}</div><div><br></div><div>// Each of the four primitive types is its own PrimitiveBitPattern:</div><div>extension UInt8 : PrimitiveBitPatternRepresentable {</div><div>    init(bitPattern: UInt8) { self = bitPattern }</div><div>    var bitPattern: UInt8 { return self }</div><div>}</div><div>extension UInt16 : PrimitiveBitPatternRepresentable {</div><div>    init(bitPattern: UInt16) { self = bitPattern }</div><div>    var bitPattern: UInt16 { return self }</div><div>}</div><div>extension UInt32 : PrimitiveBitPatternRepresentable {</div><div>    init(bitPattern: UInt32) { self = bitPattern }</div><div>    var bitPattern: UInt32 { return self }</div><div>}</div><div>extension UInt64 : PrimitiveBitPatternRepresentable {</div><div>    init(bitPattern: UInt64) { self = bitPattern }</div><div>    var bitPattern: UInt64 { return self }</div><div>}</div><div>// And their signed counterparts already have the required init, so we only</div><div>// have to add the computed property:</div><div>extension Int8 : PrimitiveBitPatternRepresentable {</div><div>    var bitPattern: UInt8 { return UInt8(bitPattern: self) }</div><div>}</div><div>extension Int16 : PrimitiveBitPatternRepresentable {</div><div>    var bitPattern: UInt16 { return UInt16(bitPattern: self) }</div><div>}</div><div>extension Int32 : PrimitiveBitPatternRepresentable {</div><div>    var bitPattern: UInt32 { return UInt32(bitPattern: self) }</div><div>}</div><div>extension Int64 : PrimitiveBitPatternRepresentable {</div><div>    var bitPattern: UInt64 { return UInt64(bitPattern: self) }</div><div>}</div><div>// The platform-dependent Int and UInt:</div><div>#if arch(x86_64) || arch(arm64)</div><div>extension Int : PrimitiveBitPatternRepresentable {</div><div>    init(bitPattern: UInt64) { self = Int(Int64(bitPattern: bitPattern)) }</div><div>    var bitPattern: UInt64 { return UInt64(UInt(bitPattern: self)) }</div><div>}</div><div>extension UInt : PrimitiveBitPatternRepresentable {</div><div>    init(bitPattern: UInt64) { self = UInt(bitPattern) }</div><div>    var bitPattern: UInt64 { return UInt64(self) }</div><div>}</div><div>#elseif arch(i386) || arch(arm)</div><div>extension Int : PrimitiveBitPatternRepresentable {</div><div>    init(bitPattern: UInt32) { self = Int(Int32(bitPattern: bitPattern)) }</div><div>    var bitPattern: UInt32 { return UInt32(UInt(bitPattern: self)) }</div><div>}</div><div>extension UInt : PrimitiveBitPatternRepresentable {</div><div>    init(bitPattern: UInt32) { self = UInt(bitPattern) }</div><div>    var bitPattern: UInt32 { return UInt32(self) }</div><div>}</div><div>#endif</div></div><div><br></div><div><br></div><div>/Jens</div></div>