<div dir="ltr">I support this idea. Probably worth mentioning that Dave Abrahams has been working on integers, specifically (<a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151214/002445.html" target="_blank">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151214/002445.html</a>):<div><div><pre style="white-space:pre-wrap;color:rgb(0,0,0)">> Related: I have been working for some time on a rewrite of all the integer types and protocols. <span style="font-family:monospace,monospace">(sic)</span> Another important goal is to make the <b>integer protocols actually useful for writing generic code</b>, instead of what they are today: implementation artifacts used only for code sharing. As another <b>litmus test</b> of the usefulness of the resulting protocols, the plan is to implement BigInt in terms of the generic operations defined on integers, and make BigInt itself conform to those protocols.</pre></div><div>I've come across this issue making a BigInt library myself, I found that I wanted shift operations in the implementation of my library, but they weren't on any protocols. I'm hoping it will be necessary to add it to the protocols for the mentioned <b>litmus test</b>.</div><div><br></div></div><div>Also (<a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151214/003652.html">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151214/003652.html</a>):</div><div><span style="color:rgb(0,0,0);white-space:pre-wrap"><br></span></div><div><span style="color:rgb(0,0,0);white-space:pre-wrap"><font face="monospace, monospace">> </font></span><span style="color:rgb(0,0,0);white-space:pre-wrap"><font face="monospace, monospace">Again I'll mention that the prototype </font></span><span style="font-family:monospace,monospace;color:rgb(0,0,0);white-space:pre-wrap">(sic) I'm already working on to </span><b style="font-family:monospace,monospace;color:rgb(0,0,0);white-space:pre-wrap">replace the integer protocols</b><span style="font-family:monospace,monospace;color:rgb(0,0,0);white-space:pre-wrap"> and types already supports any integer type appearing on the RHS of a </span><b style="font-family:monospace,monospace;color:rgb(0,0,0);white-space:pre-wrap">shift operation.</b></div><div><div><br></div></div><div>Looking at the code I can't find any shift operators in protocols, but I may have missed it. Perhaps we need to check if it's still in the plan.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 19, 2016 at 6:47 AM, Haravikk via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Hmm. Well, Int is fairly common, and encompasses more than we’re ever likely to need for shifts, so it may make sense to use Self << Int as the baseline for the shifts. As for supporting smaller types, I’m not sure how it could be done in the protocol as a specific requirement unless it was something like:</div><div><br></div><div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(112,61,170)"><span style="color:#bb2ca2">protocol</span><span style="color:#000000"> BitwiseIntegerType : </span>BitwiseOperationsType<span style="color:#000000">, </span>IntegerType<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <font color="#bb2ca2">...</font></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">func</span> << (lhs:<span style="color:#703daa">Self</span>, rhs:<span style="color:rgb(112,61,170)">SignedIntegerType</span>) -> <span style="color:#703daa">Self</span></div><span style="font-family:Menlo;font-size:11px"> </span><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">func</span><span style="font-family:Menlo;font-size:11px"> << </span><span style="font-family:Menlo;font-size:11px">(lhs:</span><span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)">Self</span><span style="font-family:Menlo;font-size:11px">, rhs:</span><span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px">UnsignedIntegerType</span><span style="font-family:Menlo;font-size:11px">) -> </span><span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)">Self</span></div><div><div style="margin:0px;font-size:11px;font-family:Menlo">}</div></div><div><br></div><div>Since those types can be easily converted to the maximum supported size and then processed accordingly, but that seems much the same as requiring a right-hand-side of Self to me, except that it requires two variations per operator.</div><div><br></div><div>I posted another discussion around simplifying casting of integers, specifically allowing implicit casts of any integer type so long as the type you’re casting to has a larger range; in other words, anywhere you can put an Int32 you could put an Int16, Int8, UInt16 or UInt8, but not a UInt32 or anything larger, as these could potentially lose information so should have a warning as normal (forcing the developer to do something explicitly).</div><div><br></div><div>Handling of integers is definitely tricky!</div><div><div class="h5"><br><div><blockquote type="cite"><div>On 18 Jan 2016, at 18:11, Jacob Bandes-Storch <<a href="mailto:jtbandes@gmail.com" target="_blank">jtbandes@gmail.com</a>> wrote:</div><br><div><div dir="ltr">I saw it mentioned somewhere that the standard library team wants to allow smaller types to be used, like <<(lhs: UInt64, rhs: UInt8). I'm not sure exactly how that would fit in here.<div class="gmail_extra">
<br><div class="gmail_quote">On Mon, Jan 18, 2016 at 4:45 AM, Haravikk via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">So I recently discovered that the shift operators no longer appear to be defined anywhere, i.e- they seem to only be implemented by convention rather than being required by a protocol, which doesn’t seem ideal. The problem with this is that I wanted to implement some bitwise operations but there’s no longer an obvious place to do this, and I ended up having to implement my own concept of a BitwiseIntegerType, as you can see in the example below:<br><div><br></div><div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(112,61,170)"><span style="color:#bb2ca2">protocol</span><span> BitwiseIntegerType : </span>BitwiseOperationsType<span>, </span>IntegerType<span> {</span></div><span style="font-family:Menlo;font-size:11px"> </span><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">init</span><span style="font-family:Menlo;font-size:11px">(</span><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">_</span><span style="font-family:Menlo;font-size:11px"> value:</span><span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)">Int</span><span style="font-family:Menlo;font-size:11px">)</span><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">init</span>(<span style="color:#bb2ca2">_</span> value:<span style="color:#703daa">UInt</span>)</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">func</span> << (lhs:<span style="color:#703daa">Self</span>, rhs:<span style="color:#703daa">Self</span>) -> <span style="color:#703daa">Self</span></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">func</span> >> (lhs:<span style="color:#703daa">Self</span>, rhs:<span style="color:#703daa">Self</span>) -> <span style="color:#703daa">Self</span></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">func</span> <<= (<span style="color:#bb2ca2">inout</span> lhs:<span style="color:#703daa">Self</span>, rhs:<span style="color:#703daa">Self</span>)</div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">func</span> >>= (<span style="color:#bb2ca2">inout</span> lhs:<span style="color:#703daa">Self</span>, rhs:<span style="color:#703daa">Self</span>)</div><div style="margin:0px;font-size:11px;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span> </span>BitwiseIntegerType<span> {</span></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">static</span> <span style="color:#bb2ca2">var</span> allOnes:<span style="color:#703daa">Self</span> { <span style="color:#bb2ca2">return</span> <span style="color:#3d1d81">~</span><span style="color:#bb2ca2">Self</span>.allZeros }</div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">static</span> <span style="color:#bb2ca2">var</span> numberOfBits:<span style="color:#703daa">UInt</span> { <span style="color:#bb2ca2">return</span> <span style="color:#703daa">UInt</span>(<span style="color:#3d1d81">sizeof</span>(<span style="color:#bb2ca2">Self</span>) * <span style="color:#272ad8">8</span>) }</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">static</span> <span style="color:#bb2ca2">var</span> mostSignificantBit:<span style="color:#703daa">Self</span> { <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">Self</span>.allOnes <span style="color:#31595d"><<</span> <span style="color:#bb2ca2">Self</span>(<span style="color:#bb2ca2">Self</span>.numberOfBits - <span style="color:#272ad8">1</span>) }</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">var</span> leadingZeros:<span style="color:#703daa">UInt</span> {</div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">self</span> <span style="color:#3d1d81">==</span> <span style="color:#bb2ca2">Self</span>.allZeros { <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">Self</span>.numberOfBits }</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">var</span> value = <span style="color:#bb2ca2">self</span></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">var</span> width = <span style="color:#bb2ca2">Self</span>.numberOfBits</div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">var</span> mask = <span style="color:#bb2ca2">Self</span>.allOnes</div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">var</span> zeros:<span style="color:#703daa">UInt</span> = <span style="color:#272ad8">0</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">while</span> (value <span style="color:#3d1d81">&</span> <span style="color:#bb2ca2">Self</span>.mostSignificantBit) <span style="color:#3d1d81">==</span> <span style="color:#bb2ca2">Self</span>.allZeros {</div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">if</span> (value <span style="color:#3d1d81">&</span> mask) <span style="color:#3d1d81">==</span> <span style="color:#bb2ca2">Self</span>.allZeros {</div><div style="margin:0px;font-size:11px;font-family:Menlo"> zeros += width</div><div style="margin:0px;font-size:11px;font-family:Menlo"> value <span style="color:#31595d"><<=</span> <span style="color:#bb2ca2">Self</span>(width)</div><div style="margin:0px;font-size:11px;font-family:Menlo"> } <span style="color:#bb2ca2">else</span> {</div><div style="margin:0px;font-size:11px;font-family:Menlo"> width /= <span style="color:#272ad8">2</span></div><div style="margin:0px;font-size:11px;font-family:Menlo"> mask <span style="color:#31595d"><<=</span> <span style="color:#bb2ca2">Self</span>(width)</div><div style="margin:0px;font-size:11px;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo"> <span style="color:#bb2ca2">return</span> zeros</div><div style="margin:0px;font-size:11px;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span> </span><span style="color:#703daa">Int</span><span> : </span>BitwiseIntegerType<span> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span> </span><span style="color:#703daa">Int8</span><span> : </span>BitwiseIntegerType<span> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span> </span><span style="color:#703daa">Int16</span><span> : </span>BitwiseIntegerType<span> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span> </span><span style="color:#703daa">Int32</span><span> : </span>BitwiseIntegerType<span> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span> </span><span style="color:#703daa">Int64</span><span> : </span>BitwiseIntegerType<span> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span> </span><span style="color:#703daa">UInt</span><span> : </span>BitwiseIntegerType<span> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span> </span><span style="color:#703daa">UInt8</span><span> : </span>BitwiseIntegerType<span> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span> </span><span style="color:#703daa">UInt16</span><span> : </span>BitwiseIntegerType<span> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span> </span><span style="color:#703daa">UInt32</span><span> : </span>BitwiseIntegerType<span> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span> </span><span style="color:#703daa">UInt64</span><span> : </span>BitwiseIntegerType<span> {}</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,132,0)"><span style="color:#703daa">Int64</span><span>.mostSignificantBit </span>// -9223372036854775808</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#272ad8">1234567</span><span>.</span>leadingZeros<span> </span><span style="color:#008400">// 43</span></div></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#008400"><br></span></div>I think that the best solution would be to add the shift operators to BitwiseOperationsType, while declaring a BitwiseIntegerType similar to what I’ve done above that groups BitwiseOperationsType with IntegerType to create a distinction between binary-based integers and integers that could be based on some other mechanism in future, as well as to declare the required initializers from Int and UInt types. This gives more flexibility in defining higher level protocol extensions that rely on the full range of bitwise operations, without having to move any of it further up (iirc some of these operators used to be in IntegerType).<div><br></div><div>Either way, the shift operators are currently declared by convention, which I don’t think is right, as they should surely be declared as a requirement somewhere.</div><div><br></div><div>Also, ignore the actual implementation of leadingZeros, it may not be the most efficient method, it’s just a useful illustration of something that can be done with the protocol declarations, I’ve also omitted warnings and such to keep things simple.</div><div><br></div><div>Just wondering what others think? One other issue I’m unsure about is that the required Int and Uint initialiizers should probably be of the truncatingBitPattern type for bitwise operations, but I wasn’t sure how to handle adding that to the types that don’t currently have these initializers (i.e- the 64-bit types that don’t need them since they can’t currently be initialized from anything bigger).</div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div>
</div></blockquote></div><br></div></div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>