<html><head><meta http-equiv="Content-Type" content="text/html charset=big5"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hello everyone, </div><div class="">I have a proposal to add an "in range¡¨ operator in swift.</div><div class=""><br class=""></div><div class="">##Current situation</div><div class=""><br class=""></div>Checking whether a value is or not within a certain range now depends on the control flow you use. for switch-statement, it is intuitive; but it is not for if-statement.<div class=""><br class=""></div><div class="">Example:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> testValue = </span><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">0.3</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; color: rgb(88, 110, 117); min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; color: rgb(41, 161, 152);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures; color: #586e75" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">testValue</span><span style="font-variant-ligatures: no-common-ligatures; color: #586e75" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><span style="color: rgb(211, 54, 130); font-variant-ligatures: no-common-ligatures;" class="">case</span><span style="color: rgb(88, 110, 117); font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="color: rgb(108, 113, 196); font-variant-ligatures: no-common-ligatures;" class="">0.0</span><span style="color: rgb(88, 110, 117); font-variant-ligatures: no-common-ligatures;" class="">..<</span><span style="color: rgb(108, 113, 196); font-variant-ligatures: no-common-ligatures;" class="">1.0</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><font color="#586e75" class="">: </font></span><span style="color: rgb(88, 110, 117);" class="">do something...</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-family: 'Fira Code'; font-size: 11px; color: rgb(211, 54, 130); font-variant-ligatures: no-common-ligatures;" class="">default</span><span style="font-family: 'Fira Code'; font-size: 11px; color: rgb(88, 110, 117); font-variant-ligatures: no-common-ligatures;" class="">: </span><font color="#586e75" face="Fira Code" class=""><span style="font-size: 11px;" class="">break</span></font></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; color: rgb(88, 110, 117);" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">to write the same logic in if-statement, you have to write like this:</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; color: rgb(41, 161, 152);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">if</span><span style="font-variant-ligatures: no-common-ligatures; color: #586e75" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">testValue</span><span style="font-variant-ligatures: no-common-ligatures; color: #586e75" class=""> >= </span><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">0.0</span><span style="font-variant-ligatures: no-common-ligatures; color: #586e75" class=""> && </span><span style="font-variant-ligatures: no-common-ligatures" class="">testValue</span><span style="font-variant-ligatures: no-common-ligatures; color: #586e75" class=""> < </span><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">1.0</span><span style="font-variant-ligatures: no-common-ligatures; color: #586e75" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span>do something...</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; color: rgb(88, 110, 117);" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">The disadvantage here:</div><div style="margin: 0px; line-height: normal;" class="">1. It¡¦s wordy;</div><div style="margin: 0px; line-height: normal;" class="">2. It¡¦s risky to write the same variable name twice, typo can occur and be ignored;</div><div style="margin: 0px; line-height: normal;" class="">3. It¡¦s inconsistent with switch-statement.</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">##Proposed solution</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Introduce a new operator to check a variable against a range.</div><div style="margin: 0px; line-height: normal;" class="">Here is a drafted operator: ~ (infix operator)</div><div style="margin: 0px; line-height: normal;" class="">With ~, the above if-statement can be rewritten like:</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code';" class=""><span style="color: rgb(211, 54, 130); font-variant-ligatures: no-common-ligatures;" class="">if</span><span style="color: rgb(88, 110, 117); font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="color: rgb(41, 161, 152); font-variant-ligatures: no-common-ligatures;" class="">testValue</span><span style="color: rgb(88, 110, 117); font-variant-ligatures: no-common-ligatures;" class=""> ~ </span><span style="color: rgb(108, 113, 196); font-variant-ligatures: no-common-ligatures;" class="">0.0</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><font color="#586e75" class="">..<</font></span><span style="color: rgb(108, 113, 196); font-variant-ligatures: no-common-ligatures;" class="">1.0</span><span style="color: rgb(88, 110, 117); font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span>do something...</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Fira Code'; color: rgb(88, 110, 117);" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">looks much better.</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">What¡¦s more, if combined with the draft proposal <span style="box-sizing: border-box; color: rgb(51, 51, 51); text-decoration: none; padding-right: 3px; margin-bottom: 2px; line-height: 1.2;" class=""><a href="https://github.com/apple/swift-evolution/pull/129" class="issue-title-link js-navigation-open" style="font-style: italic; box-sizing: border-box; color: rgb(51, 51, 51); text-decoration: none; padding-right: 3px; margin-bottom: 2px; line-height: 1.2;">Generalized range operators in `for-in`loops and other purposes</a>, this new operator would more powerful.</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="box-sizing: border-box; color: rgb(51, 51, 51); text-decoration: none; padding-right: 3px; margin-bottom: 2px; line-height: 1.2;" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="box-sizing: border-box; color: rgb(51, 51, 51); text-decoration: none; padding-right: 3px; margin-bottom: 2px; line-height: 1.2;" class="">Thanks for reading</span></div></div></div></body></html>