<div>On Sat, Nov 18, 2017 at 16:25 Benjamin G &lt;<a href="mailto:benjamin.garrigues@gmail.com">benjamin.garrigues@gmail.com</a>&gt; wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>I think because it&#39;s not immediately obvious with multiple if statement, that they all try to compare the same expression to different patterns.<div><br></div><div>match exp {</div><div>case 1</div><div>case 2</div><div>}</div><div>vs</div><div>if case 1 = exp</div><div>if case 2 = anotherexp </div></div></blockquote><div dir="auto"><br></div><div dir="auto">And this is a problem that requires a new syntax because...?</div><div dir="auto"><br></div><div dir="auto">Consider that “exp” can be mutated in case 1; now reflect whether the proposed syntax facilitates or hinders correct code.</div><div dir="auto"><br></div><div dir="auto"><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div></div></div><div class="gmail_extra"><div class="gmail_quote">On Sat, Nov 18, 2017 at 10:43 PM, Xiaodi Wu via swift-evolution <span>&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><span>On Sat, Nov 18, 2017 at 3:12 PM, Peter Kamb <span>&lt;<a href="mailto:peterkamb@gmail.com" target="_blank">peterkamb@gmail.com</a>&gt;</span> wrote:<br></span><div class="gmail_extra"><div class="gmail_quote"><span><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>A high bar for new syntax is fair and expected, and by posting I was hoping to maybe find an alternative in the comments here.<div><br></div><div>But AFAIK there&#39;s currently no ability in Swift to:</div><div><br></div><div>&quot;Evaluate a *single* control expression against all of these patterns, and execute any and all cases that match&quot;<div><div><br></div><div>Multiple `if-case` statements, each re-stating the control expression, are ok.</div><div><br></div><div>But that&#39;s definitely not as clear or concise as a switch-like construct with the single control expression at the top. Or perhaps some other alternative such as the mentioned `continue` or somehow enumerating a set of cases.</div></div></div></div></blockquote><div><br></div></span><div>You&#39;re simply restating your proposed new syntax as the thing that&#39;s missing. But what is the use case that motivates this construct? In what way are multiple if-case statements &quot;not as clear&quot;?</div><div><div class="m_-1042011099583709088h5"><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="m_-1042011099583709088m_1215296999888247609HOEnZb"><div class="m_-1042011099583709088m_1215296999888247609h5"><div class="gmail_extra"><div class="gmail_quote">On Sat, Nov 18, 2017 at 11:18 AM, Xiaodi Wu <span>&lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>Robert is quite right--I&#39;m not sure what we&#39;re designing for here. There&#39;s a very high bar for introducing new syntax and a distaste for the existing syntax is not a motivating use case.<div><div class="m_-1042011099583709088m_1215296999888247609m_8769700845806481439h5"><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Nov 18, 2017 at 12:53 PM, Kevin Nattinger via swift-evolution <span>&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">There have been earlier suggestions for an alternative to `fallthrough` that would continue matching cases; I think that is much more likely to get support than a whole new construct with only a subtle difference from an existing one—would that be an acceptable alternative to you?<br>
<div class="m_-1042011099583709088m_1215296999888247609m_8769700845806481439m_-9186521494073167498HOEnZb"><div class="m_-1042011099583709088m_1215296999888247609m_8769700845806481439m_-9186521494073167498h5"><br>
&gt; On Nov 17, 2017, at 12:06 PM, Peter Kamb via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; ## Title<br>
&gt;<br>
&gt; Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching<br>
&gt;<br>
&gt; ## Summary:<br>
&gt;<br>
&gt; The syntax of the `switch` statement is familiar, succinct, elegant, and understandable. Swift pattern-matching tutorials use `switch` statements almost exclusively, with small sections at the end for alternatives such as `if case`.<br>
&gt;<br>
&gt; However, the `switch` statement has several unique behaviors unrelated to pattern matching. Namely:<br>
&gt;<br>
&gt;  - Only the *first* matching case is executed. Subsequent matching cases are not executed.<br>
&gt;  - `default:` case is required, even for expressions where a default case does not make sense.<br>
&gt;<br>
&gt; These behaviors prevent `switch` from being used as a generic match-patterns-against-a-single-expression statement.<br>
&gt;<br>
&gt; Swift should contain an equally-good pattern-matching statement that does not limit itself single-branch switching.<br>
&gt;<br>
&gt; ## Pitch:<br>
&gt;<br>
&gt; Add a `match` statement with the same elegant syntax as the `switch` statement, but without any of the &quot;branch switching&quot; baggage.<br>
&gt;<br>
&gt; ```<br>
&gt; match someValue {<br>
&gt; case patternOne:<br>
&gt;     always executed if pattern matches<br>
&gt; case patternTwo:<br>
&gt;     always executed if pattern matches<br>
&gt; }<br>
&gt; ```<br>
&gt;<br>
&gt; The match statement would allow a single value to be filtered through *multiple* cases of pattern-matching evaluation.<br>
&gt;<br>
&gt; ## Example:<br>
&gt;<br>
&gt; ```<br>
&gt; struct TextFlags: OptionSet {<br>
&gt;     let rawValue: Int<br>
&gt;     static let italics = TextFlags(rawValue: 1 &lt;&lt; 1)<br>
&gt;     static let bold    = TextFlags(rawValue: 1 &lt;&lt; 2)<br>
&gt; }<br>
&gt;<br>
&gt; let textFlags: TextFlags = [.italics, .bold]<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; // SWITCH STATEMENT<br>
&gt; switch textFlags {<br>
&gt; case let x where x.contains(.italics):<br>
&gt;     print(&quot;italics&quot;)<br>
&gt; case let x where x.contains(.bold):<br>
&gt;     print(&quot;bold&quot;)<br>
&gt; default:<br>
&gt;     print(&quot;forced to include a default case&quot;)<br>
&gt; }<br>
&gt; // prints &quot;italics&quot;<br>
&gt; // Does NOT print &quot;bold&quot;, despite .bold being set.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; // MATCH STATEMENT<br>
&gt; match textFlags {<br>
&gt; case let x where x.contains(.italics):<br>
&gt;     print(&quot;italics&quot;)<br>
&gt; case let x where x.contains(.bold):<br>
&gt;     print(&quot;bold&quot;)<br>
&gt; }<br>
&gt; // prints &quot;italics&quot;<br>
&gt; // prints &quot;bold&quot;<br>
&gt; ```<br>
&gt;<br>
&gt; ## Enum vs. OptionSet<br>
&gt;<br>
&gt; The basic difference between `switch` and `match` is the same conceptual difference between `Emum` and an `OptionSet` bitmask.<br>
&gt;<br>
&gt; `switch` is essentially designed for enums: switching to a single logical branch based on the single distinct case represented by the enum.<br>
&gt;<br>
&gt; `match` would be designed for OptionSet bitmasks and similar constructs. Executing behavior for *any and all* of the following cases and patterns that match.<br>
&gt;<br>
&gt; The programmer would choose between `switch` or `match` based on the goal of the pattern matching. For example, pattern matching a String. `switch` would be appropriate for evaluating a String that represents the rawValue of an enum. But `match` would be more appropriate for evaluating a single input String against multiple unrelated-to-each-other regexes.<br>
&gt;<br>
&gt; ## Existing Alternatives<br>
&gt;<br>
&gt; `switch` cannot be used to match multiple cases. There are several ways &quot;test a value against multiple patterns, executing behavior for each pattern that matches&quot;, but none are as elegant and understandable as the switch statement syntax.<br>
&gt;<br>
&gt; Example using a string of independent `if case` statements:<br>
&gt;<br>
&gt; ```<br>
&gt; if case let x = textFlags, x.contains(.italics) {<br>
&gt;     print(&quot;italics&quot;)<br>
&gt; }<br>
&gt;<br>
&gt; if case let x = textFlags, x.contains(.bold) {<br>
&gt;     print(&quot;bold&quot;)<br>
&gt; }<br>
&gt; ```<br>
&gt;<br>
&gt; ## `match` statement benefits:<br>
&gt;<br>
&gt;  - Allow filtering a single object through *multiple* cases of pattern matching, executing *all* cases that match.<br>
&gt;<br>
&gt;  - A syntax that exactly aligns with the familiar, succinct, elegant, and understandable `switch` syntax.<br>
&gt;<br>
&gt; - The keyword &quot;match&quot; highlights that pattern matching will occur. Would be even better than `switch` for initial introductions to pattern-matching.<br>
&gt;<br>
&gt;  - No need to convert between the strangely slightly different syntax of `switch` vs. `if case`, such as `case let x where x.contains(.italics):` to `if case let x = textFlags, x.contains(.italics) {`<br>
&gt;<br>
&gt;  - Bring the &quot;Expression Pattern&quot; to non-branch-switching contexts. Currently: &quot;An expression pattern represents the value of an expression. Expression patterns appear only in switch statement case labels.&quot;<br>
&gt;<br>
&gt;  - A single `match controlExpression` at the top rather than `controlExpression` being repeated (and possibly changed) in every single `if case` statement.<br>
&gt;<br>
&gt;  - Duplicated `controlExpression` is an opportunity for bugs such as typos or changes to the expression being evaluated in a *single* `if case` from the set, rather than all cases.<br>
&gt;<br>
&gt;  - Reduces to a pretty elegant single-case. This one-liner is an easy &quot;just delete whitespace&quot; conversion from standard multi-line switch/match syntax, whereas `if case` is not.<br>
&gt;<br>
&gt; ```<br>
&gt;  match value { case pattern:<br>
&gt;     print(&quot;matched&quot;)<br>
&gt; }<br>
&gt; ```<br>
&gt;<br>
&gt;  - Eliminate the boilerplate `default: break` case line for non-exhaustible expressions. Pretty much any non-Enum type being evaluated is non-exhaustible. (This is not the *main* goal of this proposal.)<br>
&gt;<br>
&gt; ## Prototype<br>
&gt;<br>
&gt; A prototype `match` statement can be created in Swift by wrapping a `switch` statement in a loop and constructing each case to match only on a given iteration of the loop:<br>
&gt;<br>
&gt; ```<br>
&gt; match: for eachCase in 0...1 {<br>
&gt; switch (eachCase, textFlags) {<br>
&gt; case (0, let x) where x.contains(.italics):<br>
&gt;     print(&quot;italics&quot;)<br>
&gt; case (1, let x) where x.contains(.bold):<br>
&gt;     print(&quot;bold&quot;)<br>
&gt; default: break }<br>
&gt; }<br>
&gt;<br>
&gt; // prints &quot;italics&quot;<br>
&gt; // prints &quot;bold&quot;<br>
&gt; ```<br>
&gt;<br>
&gt; ## Notes / Discussion:<br>
&gt;<br>
&gt; - Other Languages - I&#39;ve been unable to find a switch-syntax non-&quot;switching&quot; pattern-match operator in any other language. If you know of any, please post!<br>
&gt;<br>
&gt; - Should `match` allow a `default:` case? It would be easy enough to add one that functioned like switch&#39;s default case: run if *no other* cases were executed. But, conceptually, should a &quot;match any of these patterns&quot; statement have an else/default clause? I think it should, unless there are any strong opinions.<br>
&gt;<br>
&gt; - FizzBuzz using proposed Swift `match` statement:<br>
&gt;<br>
&gt; ```<br>
&gt; for i in 1...100 {<br>
&gt;     var output = &quot;&quot;<br>
&gt;     match 0 {<br>
&gt;     case (i % 3): output += &quot;Fizz&quot;<br>
&gt;     case (i % 3): output += &quot;Buzz&quot;<br>
&gt;     default:      output = String(i)<br>
&gt;     }<br>
&gt;<br>
&gt;     print(output)<br>
&gt; }<br>
&gt;<br>
&gt; // `15` prints &quot;FizzBuzz&quot;<br>
&gt; ```<br>
</div></div><div class="m_-1042011099583709088m_1215296999888247609m_8769700845806481439m_-9186521494073167498HOEnZb"><div class="m_-1042011099583709088m_1215296999888247609m_8769700845806481439m_-9186521494073167498h5">&gt; _______________________________________________<br>
&gt; swift-evolution mailing list<br>
&gt; <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
&gt; <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>
_______________________________________________<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>
</div></div></blockquote></div><br></div></div></div></div></div>
</blockquote></div><br></div>
</div></div></blockquote></div></div></div><br></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>
</blockquote></div></div>