<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Feb 21, 2017, at 5:39 PM, Dave Abrahams <<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>> wrote:</div><div class=""><div dir="auto" class=""><div class=""><div class="">Sent from my moss-covered three-handled family gradunza</div></div><div class=""><br class="">On Feb 21, 2017, at 10:08 AM, John McCall <<a href="mailto:rjmccall@apple.com" class="">rjmccall@apple.com</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 21, 2017, at 2:15 PM, Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""><br class=""><br class=""><div class="">Sent from my moss-covered three-handled family gradunza</div></div><div class=""><br class="">On Feb 21, 2017, at 9:04 AM, Jordan Rose <<a href="mailto:jordan_rose@apple.com" class="">jordan_rose@apple.com</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class="">[Proposal: <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md" class="">https://github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md</a>]<div class=""><br class=""></div><div class="">Hi, Max (and Dave). I did have some questions about this revision:</div><div class=""><br class=""></div><div class=""><div class=""><blockquote type="cite" class="">Arithmetic and SignedArithmetic protocols have been renamed to Number and SignedNumber.<br class=""></blockquote><br class=""></div><div class="">What happens to NSNumber here? It feels like the same problem as Character and (NS)CharacterSet.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div class=""><blockquote type="cite" class="">Endian-converting initializers and properties were added to the FixedWidthInteger protocol.<br class=""></blockquote><br class=""></div>This is the thing I have the biggest problem with. Endian conversions aren't numeric operations, and you can't meaningfully mix numbers of different endianness. That implies to me that numbers with different endianness should have different types. I think there's a design to explore with LittleEndian<Int> and BigEndian<Int>, and explicitly using those types whenever you need to convert. </div></div></div></div></blockquote><div class=""><br class=""></div><div class="">I disagree. Nobody actually wants to compute with numbers in the wrong endianness for the machine. This is just used for corrections at the ends of wire protocols, where static type has no meaning.</div></div></div></blockquote><div class=""><br class=""></div>I think Jordan's suggestion is not that LittleEndian<Int> or BigEndian<Int> would be artihmetic types, but that they would be different types, primarily opaque, that can be explicitly converted to/from Int. When you read something off the wire, you ask for the bytes as one of those two types (as appropriate) and then convert to the underlying type. Ideally, Int doesn't even conform to the "this type can be read off the wire" protocol, eliminating the common mistake of serializing something using native endianness.</div></div></blockquote><div class=""><br class=""></div>Still, you have to implement those somehow. How do you do that without this functionality in the Integer API? Turtles have to stop somewhere. We could de-emphasize these APIs by making them static, but "x.yyyy" already <i class="">is</i> a place of reduced emphasis for integers.</div></div></blockquote><div><br class=""></div>That's fair. I don't object to having methods for these as long as they aren't the encouraged way of working with byte-swapped integers.</div><div><br class=""></div><div>John.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div class="">John.</div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><br class=""><blockquote type="cite" class=""><div class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class="">Here's a sketch of such a thing:</div><div class=""><br class=""></div></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class="">struct LittleEndian<Value: FixedWidthInteger> {</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> private var storage: Value</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""><br class=""></div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> public var value: Value {</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class="">#if little_endian</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> return storage</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class="">#else</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> return swapBytes(storage)</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class="">#endif</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> }</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""><br class=""></div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> public var bitPattern: Value {</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> return storage</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> }</div></div><div class=""><br class=""></div><div class=""> public var asBigEndian: BigEndian<Value> {</div><div class=""> return BigEndian(value: self.value)</div><div class=""> }</div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""><br class=""></div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> public init(value: Value) {</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class="">#if little_endian</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> storage = value</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class="">#else</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> storage = swapBytes(value)</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class="">#endif</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> }</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""><br class=""></div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> public init(bitPattern: Value) {</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> storage = bitPattern</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""> }</div></div></div><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class="">}</div></div></div></blockquote><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><div class=""><br class=""></div><div class="">I'm not saying this is the <i class="">right</i> solution, just that I suspect adding Self-producing properties that change endianness is the wrong one.</div><br class=""></div><div class=""><blockquote type="cite" class=""> /// The number of bits equal to 1 in this value's binary representation.<br class=""> ///<br class=""> /// For example, in a fixed-width integer type with a `bitWidth` value of 8,<br class=""> /// the number 31 has five bits equal to 1.<br class=""> ///<br class=""> /// let x: Int8 = 0b0001_1111<br class=""> /// // x == 31<br class=""> /// // x.popcount == 5<br class=""> var popcount: Int { get<div class=""> }</div></blockquote><br class=""></div><div class="">Is this property actually useful enough to put into a protocol? I know it's defaulted, but it's already an esoteric operation; it seems unlikely that one would need it in a generic context. (It's also definable for arbitrary UnsignedIntegers as well as arbitrary FixedWidthIntegers.)</div></div></div></blockquote><div class=""><br class=""></div><div class="">The whole point is that you want to dispatch down to an LLVM instruction for this and not rely on the optimizer to collapse your loop into one. </div><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" applecontenteditable="true" class=""><div class=""><br class=""></div><div class="">(I'm also still not happy with the non-Swifty name, but I see "populationCount" or "numberOfOneBits" would probably be worse.)</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Thanks in advance,</div><div class="">Jordan</div></div></div></blockquote></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></blockquote></div></div></div></blockquote></div><br class=""></body></html>