<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div>Having spent a lot of time with ‘switch’, I don’t understand any of the motivations or corresponding justifications for this idea. &nbsp;Comments inline:<br><br><div id="AppleMailSignature">~Robert Widmann&nbsp;</div><div><br>2017/11/17 15:06、Peter Kamb via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;のメール:<br><br></div><blockquote type="cite"><div><div dir="ltr"><div>## Title</div><div><br></div><div><div>Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching</div></div><div><br></div><div>## Summary:</div><div><br></div><div>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`.</div><div><br></div><div>However, the `switch` statement has several unique behaviors unrelated to pattern matching. Namely:&nbsp;&nbsp;</div></div></div></blockquote><div><br></div><div><br></div><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>&nbsp;- Only the *first* matching case is executed. Subsequent matching cases are not executed.</div></div></div></blockquote><div><br></div><div>This is not unrelated to pattern matching, this is the expected behavior of every pattern matching algorithm. &nbsp;The intent is to compile a switch-case tree down to (conceptually) a (hopefully minimal) if-else tree. &nbsp;You may be thinking of the behavior of switch in C and C-likes which is most certainly not pattern matching and includes behavior Swift has explicitly chosen to avoid like implicit fallthroughs.</div><br><blockquote type="cite"><div><div dir="ltr"><div>&nbsp;- `default:` case is required, even for expressions where a default case does not make sense.</div></div></div></blockquote><div><br></div><div>Expression patterns may look to be covered “at first glance”, but the analysis required to prove that is equivalent to solving the halting problem in the general case. &nbsp;Further, your proposed idea has absolutely nothing to do with this.</div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>These behaviors prevent `switch` from being used as a generic match-patterns-against-a-single-expression statement.</div><div><br></div><div>Swift should contain an equally-good pattern-matching statement that does not limit itself single-branch switching.</div><div><br></div><div>## Pitch:</div><div><br></div><div>Add a `match` statement with the same elegant syntax as the `switch` statement, but without any of the "branch switching" baggage.</div><div><br></div><div>```</div><div>match someValue {</div><div>case patternOne:</div><div>&nbsp; &nbsp; always executed if pattern matches</div><div>case patternTwo:</div><div>&nbsp; &nbsp; always executed if pattern matches</div><div>}</div><div>```</div><div><br></div><div>The match statement would allow a single value to be filtered through *multiple* cases of pattern-matching evaluation.</div><div><br></div><div>## Example:</div><div><br></div><div>```</div><div>struct TextFlags: OptionSet {</div><div>&nbsp; &nbsp; let rawValue: Int</div><div>&nbsp; &nbsp; static let italics = TextFlags(rawValue: 1 &lt;&lt; 1)</div><div>&nbsp; &nbsp; static let bold&nbsp; &nbsp; = TextFlags(rawValue: 1 &lt;&lt; 2)</div><div>}</div><div><br></div><div>let textFlags: TextFlags = [.italics, .bold]</div><div><br></div><div><br></div><div><br></div><div>// SWITCH STATEMENT</div><div>switch textFlags {</div><div>case let x where x.contains(.italics):</div><div>&nbsp; &nbsp; print("italics")</div><div>case let x where x.contains(.bold):</div><div>&nbsp; &nbsp; print("bold")</div><div>default:</div><div>&nbsp; &nbsp; print("forced to include a default case")</div><div>}</div><div>// prints "italics"</div><div>// Does NOT print "bold", despite .bold being set.</div><div><br></div><div><br></div><div><br></div><div>// MATCH STATEMENT</div><div>match textFlags {</div><div>case let x where x.contains(.italics):</div><div>&nbsp; &nbsp; print("italics")</div><div>case let x where x.contains(.bold):</div><div>&nbsp; &nbsp; print("bold")</div><div>}</div><div>// prints "italics"</div><div>// prints "bold"</div><div>```</div></div></div></blockquote><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>## Enum vs. OptionSet</div><div><br></div><div>The basic difference between `switch` and `match` is the same conceptual difference between `Emum` and an `OptionSet` bitmask.</div><div><br></div><div>`switch` is essentially designed for enums: switching to a single logical branch based on the single distinct case represented by the enum.</div><div><br></div><div>`match` would be designed for OptionSet bitmasks and similar constructs. Executing behavior for *any and all* of the following cases and patterns that match.</div><div><br></div><div>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.</div><div><br></div><div>## Existing Alternatives</div><div><br></div><div>`switch` cannot be used to match multiple cases. There are several ways "test a value against multiple patterns, executing behavior for each pattern that matches", but none are as elegant and understandable as the switch statement syntax.</div><div><br></div><div>Example using a string of independent `if case` statements:</div><div><br></div><div>```</div><div>if case let x = textFlags, x.contains(.italics) {</div><div>&nbsp; &nbsp; print("italics")</div><div>}</div><div><br></div><div>if case let x = textFlags, x.contains(.bold) {</div><div>&nbsp; &nbsp; print("bold")</div><div>}</div><div>```</div><div><br></div><div>## `match` statement benefits:</div><div><br></div><div>&nbsp;- Allow filtering a single object through *multiple* cases of pattern matching, executing *all* cases that match.</div></div></div></blockquote><div><br></div><div>Again, this is not pattern matching.</div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>&nbsp;- A syntax that exactly aligns with the familiar, succinct, elegant, and understandable `switch` syntax.</div></div></div></blockquote><div><br></div><div>A syntax that duplicates the existing if-case construct which, I should note, takes the same number of lines and roughly the same number of columns to express as your match. &nbsp;Even less, considering you unwrap in the if-case to exaggerate the example but not in the match.</div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>- The keyword "match" highlights that pattern matching will occur. Would be even better than `switch` for initial introductions to pattern-matching.</div><div><br></div><div>&nbsp;- 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) {`</div></div></div></blockquote><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>&nbsp;- Bring the "Expression Pattern" to non-branch-switching contexts. Currently: "An expression pattern represents the value of an expression. Expression patterns appear only in switch statement case labels."</div></div></div></blockquote><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>&nbsp;- A single `match controlExpression` at the top rather than `controlExpression` being repeated (and possibly changed) in every single `if case` statement.</div><div><br></div><div>&nbsp;- 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.</div><div><br></div><div>&nbsp;- Reduces to a pretty elegant single-case. This one-liner is an easy "just delete whitespace" conversion from standard multi-line switch/match syntax, whereas `if case` is not.</div><div><br></div><div>```</div><div>&nbsp;match value { case pattern:</div><div>&nbsp; &nbsp; print("matched")</div><div>}</div><div>```</div><div><br></div><div>&nbsp;- 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.)</div></div></div></blockquote><div><br></div><div>This is not boilerplate!</div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>## Prototype</div><div><br></div><div>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:</div><div><br></div><div>```</div><div>match: for eachCase in 0...1 {</div><div>switch (eachCase, textFlags) {</div><div>case (0, let x) where x.contains(.italics):</div><div>&nbsp; &nbsp; print("italics")</div><div>case (1, let x) where x.contains(.bold):</div><div>&nbsp; &nbsp; print("bold")</div><div>default: break }</div><div>}</div><div><br></div><div>// prints "italics"</div><div>// prints "bold"</div><div>```</div><div><br></div><div>## Notes / Discussion:</div><div><br></div><div>- Other Languages - I've been unable to find a switch-syntax non-"switching" pattern-match operator in any other language. If you know of any, please post!</div><div><br></div><div>- Should `match` allow a `default:` case? It would be easy enough to add one that functioned like switch's default case: run if *no other* cases were executed. But, conceptually, should a "match any of these patterns" statement have an else/default clause? I think it should, unless there are any strong opinions.</div><div><br></div><div>- FizzBuzz using proposed Swift `match` statement:</div><div><br></div><div>```</div><div>for i in 1...100 {</div><div>&nbsp; &nbsp; var output = ""</div><div>&nbsp; &nbsp; match 0 {</div><div>&nbsp; &nbsp; case (i % 3): output += "Fizz"</div><div>&nbsp; &nbsp; case (i % 3): output += "Buzz"</div><div>&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; output = String(i)</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; print(output)</div><div>}</div><div><br></div><div>// `15` prints "FizzBuzz"</div><div>```</div></div></div></blockquote><div><br></div>for i in 1...100 {</div><div>&nbsp; var output = “”</div><div>&nbsp; output += (i % 3 == 0) ? “Fizz” : “”</div><div>&nbsp; <span style="background-color: rgba(255, 255, 255, 0);">output += (i % 5 == 0) ? “Buzz” : “”</span></div><div>&nbsp; print(output.isEmpty ? “\(i)” : output)</div><div>}</div><div><br></div><div>If control flow should branch multiple times, <b>why not write just write it that way!</b></div><div><br><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></body></html>