<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>You can stride through the maximum value of a type if you land right on it:</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">for i in UInt8.min.stride(through: UInt8.max, by: 1) {</div><div id="AppleMailSignature"> print(i)</div><div id="AppleMailSignature">}</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">Nate</div><div><br>On Apr 8, 2016, at 9:21 AM, Erica Sadun <<a href="mailto:erica@ericasadun.com">erica@ericasadun.com</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><br class=""><div><blockquote type="cite" class=""><div class="">On Apr 8, 2016, at 3:48 AM, tuuranton--- via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@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 class=""><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">print(UInt8.min) //0</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">print(UInt8.max) //255</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class=""><br class=""></p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">//Is there an easy way to loop between all values</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">//between (and including both) UInt8.min and UInt8.max?</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class=""><br class=""></p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">//This doesn't work.</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">//Runtime crash because UInt8.max has no successor.</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">for i in UInt8.min...UInt8.max {</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class=""> print(i)</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">}</p><div class=""><br class=""></div></div></div></blockquote><br class=""></div><div>Easy? No. </div><div><br class=""></div><div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// Range Index has no valid successor</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> i </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures" class="">.min ... </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures" class="">.max {</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(i)</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// '...' cannot be applied to two 'UInt8' operands</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">for</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> i </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">in</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="color: rgb(112, 61, 170); font-variant-ligatures: no-common-ligatures;" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.min ... </span><span style="color: rgb(112, 61, 170); font-variant-ligatures: no-common-ligatures;" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.max </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">as</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="color: rgb(112, 61, 170); font-variant-ligatures: no-common-ligatures;" class="">ClosedInterval </span><span style="font-variant-ligatures: no-common-ligatures;" class="">{</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> print(i)</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></div><div class=""><span style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 18px;" class="">// Stops at 254</span></div><div class=""><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> i </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures" class="">.min ..< </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures" class="">.max {</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(i)</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><br class=""></div><div class="">This works, but ugh:</div></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> i : </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">numericCast</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures" class="">.min)...</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">numericCast</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures" class="">.max) {</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">guard</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> j: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures" class="">(i) </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">else</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">fatalError</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Nope"</span><span style="font-variant-ligatures: no-common-ligatures" class="">) }</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(j)</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="font-family: Palatino; font-size: 14px;" class="">cc'ing in a few people to the white courtesy range phone.</div><div style="font-family: Palatino; font-size: 14px;" class=""><br class=""></div><div style="font-family: Palatino; font-size: 14px;" class="">-- E</div><div style="font-family: Palatino; font-size: 14px;" class=""><br class=""></div><div class=""><br class=""></div></span></div></div></blockquote></body></html>