<html><body><div>Am 08. April 2016 um 03:14 schrieb Harlan Haskins via swift-evolution <swift-evolution@swift.org>:<br><br><div><blockquote type="cite"><div class="msg-quote" style="word-wrap: break-word;" data-mce-style="word-wrap: break-word;">I’ve found that .contains works well for all my uses.<div class=""><br class=""></div><div class=""><span style="font-family: Menlo;" data-mce-style="font-family: Menlo;" face="Menlo">(0..<100).contains(x)</span></div></div></blockquote></div><div><span><br data-mce-bogus="1"></span></div><div><span>That's very readable and totally sufficient IMO.</span></div><div><span><br data-mce-bogus="1"></span></div><div><span>-Thorsten</span></div><div><span><br data-mce-bogus="1"></span></div><div><span><br></span><blockquote type="cite"><div class="msg-quote" style="word-wrap: break-word;" data-mce-style="word-wrap: break-word;"><div class=""><span style="font-family: Menlo;" data-mce-style="font-family: Menlo;" face="Menlo"></span></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Apr 7, 2016, at 1:17 PM, Maury Markowitz via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="" data-mce-href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">I originally posted this in swift-users, and it garnered some level of positive reaction, so I thought I would try it again here.<br class=""><br class="">We all constantly write code that checks a value against arbitrary ranges - subsets of an array, characters within a string, etc. This is a common example:<br class=""><br class="">if myVal >= oneLimit && myVar <= twoLimit { // something }<br class=""><br class="">Swift introduced a very nice range system that is used to represent these sorts of spans-within-limits, which greatly clarifies common code like this...<br class=""><br class="">for c in cards[0..<10] { // something }<br class=""><br class="">My proposal is that this same "in" syntax be allowed in an if statement. Thus the limit check above would become:<br class=""><br class="">if myVal in [oneLimit...twoLimit] { // something }<br class=""><br class="">The code is somewhat more terse, which is a common goal in Swift. It also more clearly states what the purpose of the code - this is a range check, not some arbitrary mathematical comparisons. It also has the advantage that if myVal is expensive, a func for instance, it only gets evaluated once. Effectively it replaces:<br class=""><br class="">let myVal = someExpensiveFunction()<br class="">if myVal >= oneLimit && myVar <= twoLimit...<br class=""><br class="">with a single line of code.<br class=""><br class="">I note that there are ways to accomplish this already in Swift 2, but I find them unsatisfying, and somewhat unnatural. They are definitely not "discoverable". which I believe the in statement would be. The current solution is the pattern-matching if, like this:<br class=""><br class="">if case 0...100 = someInteger<br class=""><br class="">or<br class=""><br class="">if 0...100 ~= someInteger<br class=""><br class="">I see two problems with this approach. Once is that the "if case" structure strikes me somewhat odd on it's own, but more specifically I think everyone finds the syntax "backward", we normally code the tested item on the left and limits on the right, and this reversal seems unnatural to me (in spite of it being identical in code terms). But I think the real issue is that both examples require special syntax that is very different than other languages or even most constructs in Swift itself, whereas in is already used in exactly the fashion I propose.<br class=""><br class="">I don't believe that re-using "in" would greatly burden the language, while offering the same behaviour as the pattern-matching-if in a much more natural and already-used syntax.<br class=""><br class=""><br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="" data-mce-href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" data-mce-href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></div></blockquote></div><br class=""></div></div></blockquote></div></div></body></html>