<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">You can do this:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> testValue = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0.3</span></div><div style="margin: 0px; line-height: normal; min-height: 16px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class="">// Using instance method.</div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> (<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0.0</span>..&lt;<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1.0</span>).<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">contains</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">testValue</span>) {</div><div style="margin: 0px; line-height: normal; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span>"Hello, world."<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; line-height: normal;" class="">}</div><div style="margin: 0px; line-height: normal; min-height: 16px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class="">// Using pattern matching operator.</div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0.0</span>..&lt;<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1.0</span> ~= <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">testValue</span> {</div><div style="margin: 0px; line-height: normal; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span>"Hello, world."<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; line-height: normal;" class="">}</div></div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div></div></div></div><div class="">Preston</div><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 2, 2016, at 4:53 AM, Gmail 2 via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=big5" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hello everyone,&nbsp;</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 &nbsp;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="">..&lt;</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="">:&nbsp;</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="">:&nbsp;</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=""> &gt;= </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=""> &amp;&amp; </span><span style="font-variant-ligatures: no-common-ligatures" class="">testValue</span><span style="font-variant-ligatures: no-common-ligatures; color: #586e75" class=""> &lt; </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="">&nbsp; &nbsp;&nbsp;</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="">&nbsp;</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="">&nbsp;~&nbsp;</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="">..&lt;</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="">&nbsp;{</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="">&nbsp; &nbsp;&nbsp;</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&nbsp;<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></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>