<div dir="ltr">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's currently no ability in Swift to:</div><div><br></div><div>"Evaluate a *single* control expression against all of these patterns, and execute any and all cases that match"<div><div><br></div><div>Multiple `if-case` statements, each re-stating the control expression, are ok.</div><div><br></div><div>But that'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><br></div><div><br></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Nov 18, 2017 at 11:18 AM, Xiaodi Wu <span dir="ltr"><<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Robert is quite right--I'm not sure what we're designing for here. There'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="h5"><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 dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></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_-9186521494073167498HOEnZb"><div class="m_-9186521494073167498h5"><br>
> On Nov 17, 2017, at 12:06 PM, Peter Kamb via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br>
><br>
> ## Title<br>
><br>
> Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching<br>
><br>
> ## Summary:<br>
><br>
> 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>
><br>
> However, the `switch` statement has several unique behaviors unrelated to pattern matching. Namely:<br>
><br>
> - Only the *first* matching case is executed. Subsequent matching cases are not executed.<br>
> - `default:` case is required, even for expressions where a default case does not make sense.<br>
><br>
> These behaviors prevent `switch` from being used as a generic match-patterns-against-a-singl<wbr>e-expression statement.<br>
><br>
> Swift should contain an equally-good pattern-matching statement that does not limit itself single-branch switching.<br>
><br>
> ## Pitch:<br>
><br>
> Add a `match` statement with the same elegant syntax as the `switch` statement, but without any of the "branch switching" baggage.<br>
><br>
> ```<br>
> match someValue {<br>
> case patternOne:<br>
> always executed if pattern matches<br>
> case patternTwo:<br>
> always executed if pattern matches<br>
> }<br>
> ```<br>
><br>
> The match statement would allow a single value to be filtered through *multiple* cases of pattern-matching evaluation.<br>
><br>
> ## Example:<br>
><br>
> ```<br>
> struct TextFlags: OptionSet {<br>
> let rawValue: Int<br>
> static let italics = TextFlags(rawValue: 1 << 1)<br>
> static let bold = TextFlags(rawValue: 1 << 2)<br>
> }<br>
><br>
> let textFlags: TextFlags = [.italics, .bold]<br>
><br>
><br>
><br>
> // SWITCH STATEMENT<br>
> switch textFlags {<br>
> case let x where x.contains(.italics):<br>
> print("italics")<br>
> case let x where x.contains(.bold):<br>
> print("bold")<br>
> default:<br>
> print("forced to include a default case")<br>
> }<br>
> // prints "italics"<br>
> // Does NOT print "bold", despite .bold being set.<br>
><br>
><br>
><br>
> // MATCH STATEMENT<br>
> match textFlags {<br>
> case let x where x.contains(.italics):<br>
> print("italics")<br>
> case let x where x.contains(.bold):<br>
> print("bold")<br>
> }<br>
> // prints "italics"<br>
> // prints "bold"<br>
> ```<br>
><br>
> ## Enum vs. OptionSet<br>
><br>
> The basic difference between `switch` and `match` is the same conceptual difference between `Emum` and an `OptionSet` bitmask.<br>
><br>
> `switch` is essentially designed for enums: switching to a single logical branch based on the single distinct case represented by the enum.<br>
><br>
> `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>
><br>
> 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>
><br>
> ## Existing Alternatives<br>
><br>
> `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.<br>
><br>
> Example using a string of independent `if case` statements:<br>
><br>
> ```<br>
> if case let x = textFlags, x.contains(.italics) {<br>
> print("italics")<br>
> }<br>
><br>
> if case let x = textFlags, x.contains(.bold) {<br>
> print("bold")<br>
> }<br>
> ```<br>
><br>
> ## `match` statement benefits:<br>
><br>
> - Allow filtering a single object through *multiple* cases of pattern matching, executing *all* cases that match.<br>
><br>
> - A syntax that exactly aligns with the familiar, succinct, elegant, and understandable `switch` syntax.<br>
><br>
> - The keyword "match" highlights that pattern matching will occur. Would be even better than `switch` for initial introductions to pattern-matching.<br>
><br>
> - 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>
><br>
> - 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."<br>
><br>
> - A single `match controlExpression` at the top rather than `controlExpression` being repeated (and possibly changed) in every single `if case` statement.<br>
><br>
> - 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>
><br>
> - 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.<br>
><br>
> ```<br>
> match value { case pattern:<br>
> print("matched")<br>
> }<br>
> ```<br>
><br>
> - 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>
><br>
> ## Prototype<br>
><br>
> 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>
><br>
> ```<br>
> match: for eachCase in 0...1 {<br>
> switch (eachCase, textFlags) {<br>
> case (0, let x) where x.contains(.italics):<br>
> print("italics")<br>
> case (1, let x) where x.contains(.bold):<br>
> print("bold")<br>
> default: break }<br>
> }<br>
><br>
> // prints "italics"<br>
> // prints "bold"<br>
> ```<br>
><br>
> ## Notes / Discussion:<br>
><br>
> - 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!<br>
><br>
> - 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.<br>
><br>
> - FizzBuzz using proposed Swift `match` statement:<br>
><br>
> ```<br>
> for i in 1...100 {<br>
> var output = ""<br>
> match 0 {<br>
> case (i % 3): output += "Fizz"<br>
> case (i % 3): output += "Buzz"<br>
> default: output = String(i)<br>
> }<br>
><br>
> print(output)<br>
> }<br>
><br>
> // `15` prints "FizzBuzz"<br>
> ```<br>
</div></div><div class="m_-9186521494073167498HOEnZb"><div class="m_-9186521494073167498h5">> ______________________________<wbr>_________________<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/mailma<wbr>n/listinfo/swift-evolution</a><br>
<br>
______________________________<wbr>_________________<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/mailma<wbr>n/listinfo/swift-evolution</a><br>
</div></div></blockquote></div><br></div></div></div></div></div>
</blockquote></div><br></div>