Re: [swift-evolution] [Idea] [Pitch] Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching

2017-11-18 Thread Robert Widmann via swift-evolution
In regards to the problem in the root (using if-cases to do membership tests on 
option sets), the code given there involving thinking of the OptionSet as a 
sequence and iterating over it makes this far less likely to happen.  I think 
this is a pattern that doesn't need syntax, it needs stdlib support at most.  
Perhaps an extension to OptionSet when the RawValue is known to be a sized 
integral type that does just that would clean up here.

~Robert Widmann 

2017/11/18 17:25、Benjamin G via swift-evolution のメール:

> I think because it's not immediately obvious with multiple if statement, that 
> they all try to compare the same expression to different patterns.
> 
> match exp {
> case 1
> case 2
> }
> vs
> if case 1 = exp
> if case 2 = anotherexp 
> 
> 
>> On Sat, Nov 18, 2017 at 10:43 PM, Xiaodi Wu via swift-evolution 
>>  wrote:
>>> On Sat, Nov 18, 2017 at 3:12 PM, Peter Kamb  wrote:
>> 
>>> 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.
>>> 
>>> But AFAIK there's currently no ability in Swift to:
>>> 
>>> "Evaluate a *single* control expression against all of these patterns, and 
>>> execute any and all cases that match"
>>> 
>>> Multiple `if-case` statements, each re-stating the control expression, are 
>>> ok.
>>> 
>>> 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.
>> 
>> You're simply restating your proposed new syntax as the thing that's 
>> missing. But what is the use case that motivates this construct? In what way 
>> are multiple if-case statements "not as clear"?
>> 
 On Sat, Nov 18, 2017 at 11:18 AM, Xiaodi Wu  wrote:
 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.
 
 
> On Sat, Nov 18, 2017 at 12:53 PM, Kevin Nattinger via swift-evolution 
>  wrote:
> 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?
> 
> > On Nov 17, 2017, at 12:06 PM, Peter Kamb via swift-evolution 
> >  wrote:
> >
> > ## Title
> >
> > Add `match` statement as `switch`-like syntax alternative to `if case` 
> > pattern matching
> >
> > ## Summary:
> >
> > 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`.
> >
> > However, the `switch` statement has several unique behaviors unrelated 
> > to pattern matching. Namely:
> >
> >  - Only the *first* matching case is executed. Subsequent matching 
> > cases are not executed.
> >  - `default:` case is required, even for expressions where a default 
> > case does not make sense.
> >
> > These behaviors prevent `switch` from being used as a generic 
> > match-patterns-against-a-single-expression statement.
> >
> > Swift should contain an equally-good pattern-matching statement that 
> > does not limit itself single-branch switching.
> >
> > ## Pitch:
> >
> > Add a `match` statement with the same elegant syntax as the `switch` 
> > statement, but without any of the "branch switching" baggage.
> >
> > ```
> > match someValue {
> > case patternOne:
> > always executed if pattern matches
> > case patternTwo:
> > always executed if pattern matches
> > }
> > ```
> >
> > The match statement would allow a single value to be filtered through 
> > *multiple* cases of pattern-matching evaluation.
> >
> > ## Example:
> >
> > ```
> > struct TextFlags: OptionSet {
> > let rawValue: Int
> > static let italics = TextFlags(rawValue: 1 << 1)
> > static let bold= TextFlags(rawValue: 1 << 2)
> > }
> >
> > let textFlags: TextFlags = [.italics, .bold]
> >
> >
> >
> > // SWITCH STATEMENT
> > switch textFlags {
> > case let x where x.contains(.italics):
> > print("italics")
> > case let x where x.contains(.bold):
> > print("bold")
> > default:
> > print("forced to include a default case")
> > }
> > // prints "italics"
> > // Does NOT print "bold", 

Re: [swift-evolution] [Pitch] Improving capturing semantics of local functions

2017-11-18 Thread Howard Lovatt via swift-evolution
I think you have changed my mind. Virtually everything I want to do can be
achieved by having a closure as an alternative syntax for a function.
Particularly if you could assign to a `var`. You still need extra syntax
for generics and escaping closures.

On Fri, 17 Nov 2017 at 1:45 pm, Slava Pestov  wrote:

>
> On Nov 16, 2017, at 4:38 PM, Howard Lovatt 
> wrote:
>
> When the user writes:
>
> let increment: (T) throws -> T where T: Numeric = { $0 + 1 }
> increment(1) // 2
> increment(1.1) // 2.1
>
>
> This means that ‘increment’ is a *value* with a generic function type.
> Presumably you want to pass generic closures as function parameters and
> results too. This is called higher-rank polymorphism and it introduces
> considerable complexity in type checking and code generation.
>
> Compiler issues global struct as above. Then:
>
> let _int_increment = _Function1__T1__T1__T1__E__Numeric({ $0 + 1
> })
> try _int_increment.call(1) // 2
> let _double_increment = _Function1__T1__T1__T1__E__Numeric({
> $0 + 1 })
> try _double_increment.call(1.1) // 2.1
>
>
> What if I do,
>
> let array = [increment]
>
> What is the type of ‘array’?
>
> Slava
>
>
> The more restrictive form that you suggest (I think this is what you mean
> anyway) of only allowed locally, not globally, is easier to name mangle,
> you just need a unique name, nothing about the name needs to be canonical.
> This would be similar to local functions at present and would be useful
> (though I am not sure how many local *generic* functions there are).
>
>
>
>   -- Howard.
>
> On 17 November 2017 at 10:47, Slava Pestov  wrote:
>
>>
>>
>> On Nov 16, 2017, at 3:07 PM, Howard Lovatt via swift-evolution <
>> swift-evolution@swift.org> wrote:
>>
>> Where I am proposing a change is that if a closure with generic arguments
>> is encountered it is transformed into the equivalent struct and the struct
>> is typed as it currently is (or if there is a better implementation
>> something equivalent to this), therefore zero change to the type system.
>>
>>
>> Since we already have local functions that can capture values and be
>> generic, there’s no need to implement a new mechanism for name mangling or
>> handling of captures.
>>
>>
>> The changes proposed are a transformation into a struct and name
>> mangling, e.g.:
>>
>> let increment: (T) throws -> T where T: Numeric = { $0 + 1 }
>> let increment = { (n: T) throws -> T where T: Numeric in n + 1 }
>> let increment: (T) throws -> T where T: Numeric = { (n: T)
>> throws -> T where T: Numeric in n + 1 }
>>
>>
>> It sounds like what you’re proposing is essentially a new surface syntax
>> for local functions — since a generic closure would not be a first class
>> value, it could not appear anywhere except for the right hand side of a let
>> binding, right?
>>
>> Slava
>>
>
> --
-- Howard.
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] [Idea] [Pitch] Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching

2017-11-18 Thread Xiaodi Wu via swift-evolution
On Sat, Nov 18, 2017 at 16:25 Benjamin G 
wrote:

> I think because it's not immediately obvious with multiple if statement,
> that they all try to compare the same expression to different patterns.
>
> match exp {
> case 1
> case 2
> }
> vs
> if case 1 = exp
> if case 2 = anotherexp
>

And this is a problem that requires a new syntax because...?

Consider that “exp” can be mutated in case 1; now reflect whether the
proposed syntax facilitates or hinders correct code.


On Sat, Nov 18, 2017 at 10:43 PM, Xiaodi Wu via swift-evolution <
> swift-evolution@swift.org> wrote:
>
>> On Sat, Nov 18, 2017 at 3:12 PM, Peter Kamb  wrote:
>>
>>> 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.
>>>
>>> But AFAIK there's currently no ability in Swift to:
>>>
>>> "Evaluate a *single* control expression against all of these patterns,
>>> and execute any and all cases that match"
>>>
>>> Multiple `if-case` statements, each re-stating the control expression,
>>> are ok.
>>>
>>> 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.
>>>
>>
>> You're simply restating your proposed new syntax as the thing that's
>> missing. But what is the use case that motivates this construct? In what
>> way are multiple if-case statements "not as clear"?
>>
>> On Sat, Nov 18, 2017 at 11:18 AM, Xiaodi Wu  wrote:
>>>
 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.


 On Sat, Nov 18, 2017 at 12:53 PM, Kevin Nattinger via swift-evolution <
 swift-evolution@swift.org> wrote:

> 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?
>
> > On Nov 17, 2017, at 12:06 PM, Peter Kamb via swift-evolution <
> swift-evolution@swift.org> wrote:
> >
> > ## Title
> >
> > Add `match` statement as `switch`-like syntax alternative to `if
> case` pattern matching
> >
> > ## Summary:
> >
> > 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`.
> >
> > However, the `switch` statement has several unique behaviors
> unrelated to pattern matching. Namely:
> >
> >  - Only the *first* matching case is executed. Subsequent matching
> cases are not executed.
> >  - `default:` case is required, even for expressions where a default
> case does not make sense.
> >
> > These behaviors prevent `switch` from being used as a generic
> match-patterns-against-a-single-expression statement.
> >
> > Swift should contain an equally-good pattern-matching statement that
> does not limit itself single-branch switching.
> >
> > ## Pitch:
> >
> > Add a `match` statement with the same elegant syntax as the `switch`
> statement, but without any of the "branch switching" baggage.
> >
> > ```
> > match someValue {
> > case patternOne:
> > always executed if pattern matches
> > case patternTwo:
> > always executed if pattern matches
> > }
> > ```
> >
> > The match statement would allow a single value to be filtered
> through *multiple* cases of pattern-matching evaluation.
> >
> > ## Example:
> >
> > ```
> > struct TextFlags: OptionSet {
> > let rawValue: Int
> > static let italics = TextFlags(rawValue: 1 << 1)
> > static let bold= TextFlags(rawValue: 1 << 2)
> > }
> >
> > let textFlags: TextFlags = [.italics, .bold]
> >
> >
> >
> > // SWITCH STATEMENT
> > switch textFlags {
> > case let x where x.contains(.italics):
> > print("italics")
> > case let x where x.contains(.bold):
> > print("bold")
> > default:
> > print("forced to include a default case")
> > }
> > // prints "italics"
> > // Does NOT print "bold", despite .bold being set.
> >
> >
> >
> > // MATCH STATEMENT
> > match textFlags {
> > case let x where x.contains(.italics):
> > print("italics")
> > case let x where x.contains(.bold):
> > print("bold")
> > }
> > // prints "italics"
> > // prints "bold"
> > ```
> >

Re: [swift-evolution] [Idea] [Pitch] Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching

2017-11-18 Thread Benjamin G via swift-evolution
I think because it's not immediately obvious with multiple if statement,
that they all try to compare the same expression to different patterns.

match exp {
case 1
case 2
}
vs
if case 1 = exp
if case 2 = anotherexp


On Sat, Nov 18, 2017 at 10:43 PM, Xiaodi Wu via swift-evolution <
swift-evolution@swift.org> wrote:

> On Sat, Nov 18, 2017 at 3:12 PM, Peter Kamb  wrote:
>
>> 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.
>>
>> But AFAIK there's currently no ability in Swift to:
>>
>> "Evaluate a *single* control expression against all of these patterns,
>> and execute any and all cases that match"
>>
>> Multiple `if-case` statements, each re-stating the control expression,
>> are ok.
>>
>> 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.
>>
>
> You're simply restating your proposed new syntax as the thing that's
> missing. But what is the use case that motivates this construct? In what
> way are multiple if-case statements "not as clear"?
>
> On Sat, Nov 18, 2017 at 11:18 AM, Xiaodi Wu  wrote:
>>
>>> 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.
>>>
>>>
>>> On Sat, Nov 18, 2017 at 12:53 PM, Kevin Nattinger via swift-evolution <
>>> swift-evolution@swift.org> wrote:
>>>
 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?

 > On Nov 17, 2017, at 12:06 PM, Peter Kamb via swift-evolution <
 swift-evolution@swift.org> wrote:
 >
 > ## Title
 >
 > Add `match` statement as `switch`-like syntax alternative to `if
 case` pattern matching
 >
 > ## Summary:
 >
 > 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`.
 >
 > However, the `switch` statement has several unique behaviors
 unrelated to pattern matching. Namely:
 >
 >  - Only the *first* matching case is executed. Subsequent matching
 cases are not executed.
 >  - `default:` case is required, even for expressions where a default
 case does not make sense.
 >
 > These behaviors prevent `switch` from being used as a generic
 match-patterns-against-a-single-expression statement.
 >
 > Swift should contain an equally-good pattern-matching statement that
 does not limit itself single-branch switching.
 >
 > ## Pitch:
 >
 > Add a `match` statement with the same elegant syntax as the `switch`
 statement, but without any of the "branch switching" baggage.
 >
 > ```
 > match someValue {
 > case patternOne:
 > always executed if pattern matches
 > case patternTwo:
 > always executed if pattern matches
 > }
 > ```
 >
 > The match statement would allow a single value to be filtered through
 *multiple* cases of pattern-matching evaluation.
 >
 > ## Example:
 >
 > ```
 > struct TextFlags: OptionSet {
 > let rawValue: Int
 > static let italics = TextFlags(rawValue: 1 << 1)
 > static let bold= TextFlags(rawValue: 1 << 2)
 > }
 >
 > let textFlags: TextFlags = [.italics, .bold]
 >
 >
 >
 > // SWITCH STATEMENT
 > switch textFlags {
 > case let x where x.contains(.italics):
 > print("italics")
 > case let x where x.contains(.bold):
 > print("bold")
 > default:
 > print("forced to include a default case")
 > }
 > // prints "italics"
 > // Does NOT print "bold", despite .bold being set.
 >
 >
 >
 > // MATCH STATEMENT
 > match textFlags {
 > case let x where x.contains(.italics):
 > print("italics")
 > case let x where x.contains(.bold):
 > print("bold")
 > }
 > // prints "italics"
 > // prints "bold"
 > ```
 >
 > ## Enum vs. OptionSet
 >
 > The basic difference between `switch` and `match` is the same
 conceptual difference between `Emum` and an `OptionSet` bitmask.
 >
 > `switch` is essentially designed for enums: switching to a single
 logical branch based on the single distinct case represented by the enum.
 >
 > `match` would be designed for OptionSet bitmasks and similar
 constructs. 

Re: [swift-evolution] [Idea] [Pitch] Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching

2017-11-18 Thread Xiaodi Wu via swift-evolution
On Sat, Nov 18, 2017 at 3:12 PM, Peter Kamb  wrote:

> 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.
>
> But AFAIK there's currently no ability in Swift to:
>
> "Evaluate a *single* control expression against all of these patterns, and
> execute any and all cases that match"
>
> Multiple `if-case` statements, each re-stating the control expression, are
> ok.
>
> 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.
>

You're simply restating your proposed new syntax as the thing that's
missing. But what is the use case that motivates this construct? In what
way are multiple if-case statements "not as clear"?

On Sat, Nov 18, 2017 at 11:18 AM, Xiaodi Wu  wrote:
>
>> 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.
>>
>>
>> On Sat, Nov 18, 2017 at 12:53 PM, Kevin Nattinger via swift-evolution <
>> swift-evolution@swift.org> wrote:
>>
>>> 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?
>>>
>>> > On Nov 17, 2017, at 12:06 PM, Peter Kamb via swift-evolution <
>>> swift-evolution@swift.org> wrote:
>>> >
>>> > ## Title
>>> >
>>> > Add `match` statement as `switch`-like syntax alternative to `if case`
>>> pattern matching
>>> >
>>> > ## Summary:
>>> >
>>> > 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`.
>>> >
>>> > However, the `switch` statement has several unique behaviors unrelated
>>> to pattern matching. Namely:
>>> >
>>> >  - Only the *first* matching case is executed. Subsequent matching
>>> cases are not executed.
>>> >  - `default:` case is required, even for expressions where a default
>>> case does not make sense.
>>> >
>>> > These behaviors prevent `switch` from being used as a generic
>>> match-patterns-against-a-single-expression statement.
>>> >
>>> > Swift should contain an equally-good pattern-matching statement that
>>> does not limit itself single-branch switching.
>>> >
>>> > ## Pitch:
>>> >
>>> > Add a `match` statement with the same elegant syntax as the `switch`
>>> statement, but without any of the "branch switching" baggage.
>>> >
>>> > ```
>>> > match someValue {
>>> > case patternOne:
>>> > always executed if pattern matches
>>> > case patternTwo:
>>> > always executed if pattern matches
>>> > }
>>> > ```
>>> >
>>> > The match statement would allow a single value to be filtered through
>>> *multiple* cases of pattern-matching evaluation.
>>> >
>>> > ## Example:
>>> >
>>> > ```
>>> > struct TextFlags: OptionSet {
>>> > let rawValue: Int
>>> > static let italics = TextFlags(rawValue: 1 << 1)
>>> > static let bold= TextFlags(rawValue: 1 << 2)
>>> > }
>>> >
>>> > let textFlags: TextFlags = [.italics, .bold]
>>> >
>>> >
>>> >
>>> > // SWITCH STATEMENT
>>> > switch textFlags {
>>> > case let x where x.contains(.italics):
>>> > print("italics")
>>> > case let x where x.contains(.bold):
>>> > print("bold")
>>> > default:
>>> > print("forced to include a default case")
>>> > }
>>> > // prints "italics"
>>> > // Does NOT print "bold", despite .bold being set.
>>> >
>>> >
>>> >
>>> > // MATCH STATEMENT
>>> > match textFlags {
>>> > case let x where x.contains(.italics):
>>> > print("italics")
>>> > case let x where x.contains(.bold):
>>> > print("bold")
>>> > }
>>> > // prints "italics"
>>> > // prints "bold"
>>> > ```
>>> >
>>> > ## Enum vs. OptionSet
>>> >
>>> > The basic difference between `switch` and `match` is the same
>>> conceptual difference between `Emum` and an `OptionSet` bitmask.
>>> >
>>> > `switch` is essentially designed for enums: switching to a single
>>> logical branch based on the single distinct case represented by the enum.
>>> >
>>> > `match` would be designed for OptionSet bitmasks and similar
>>> constructs. Executing behavior for *any and all* of the following cases and
>>> patterns that match.
>>> >
>>> > 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 

Re: [swift-evolution] [Idea] [Pitch] Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching

2017-11-18 Thread Peter Kamb via swift-evolution
I understand your point about multiple cases + the halting problem when
applied to the implementation details of switch and pattern matching.

I think where we're missing is that what I'm envisioning would be syntactic
sugar that would be de-sugared into a a line of  single-case `if-case`
statements. It would have nothing to do with matching multiple cases via
the same mechanism as `switch`.

Motivation for the change vs. a line of if-case statements is a valid
question of course :)

On Sat, Nov 18, 2017 at 10:48 AM, Robert Widmann 
wrote:

>
> On Nov 18, 2017, at 4:42 AM, Peter Kamb  wrote:
>
> Thanks for the review.
>
> Motivation is essentially to provide a low-friction way to write `if case
> / if case / if case` statements that are all matching on the same
> expression, much in the same way that `switch` offers a way to write `if /
> else if / else` statements against the same expression.
>
>
> That much I understand, but there’s no motivation as to why this kind of
> change needs to occur over a line of if-case statements.  There isn’t
> anything in the pitch that demonstrates friction outside of a personal
> distaste for the syntax.
>
>
>  > > Only the *first* matching case is executed. Subsequent matching cases
> are not executed.
>  > This is not unrelated to pattern matching, this is the expected
> behavior of every pattern matching algorithm.
>
> By that I meant that a string of `if case / if case / if case` statements,
> each individually matching against the same expression, would match and
> execute all 3 cases. A `switch` using the same 3 cases would execute only
> the first matching case. I took the "only first case" behavior to be a
> property of the *switch*, not of "pattern matching" in general? But perhaps
> I'm using the wrong terminology.
>
>  > > Allow filtering a single object through *multiple* cases of pattern
> matching, executing *all* cases that match.
>  > Again, this is not pattern matching.
>
> A switch can have multiple cases that "would" match and execute, if you
> were to comment out the successful case above them. Why would a
> hypothetical statement that executed *all* matching cases, rather than just
> the first case, not be pattern matching? It wouldn't be a `switch`, for
> sure, but it seems like it would be just as much "pattern matching" as an
> `if-case`.
>
>
> It is not pattern matching because it violates minimality - and we have a
> warning today when you write switches of overlapping patterns.  Consider
> the semantics of pattern matching in the context of a tree - but it just so
> happens the nodes of this tree are your patterns and the branches, actual
> branches where control flow splits along the case-matrix.  The goal is not
> to execute a find-all, the goal is to execute a find - exactly like a
> regular expression.
>
>
>  > This is not boilerplate!
>
> An `if case / if case` pair would not have the same concept of a shared
> `else/default` case used by a `switch` or `if/else`, which is why I said it
> would not be needed. `if-case` does not require an else/default. (Perhaps
> boilerplate was the wrong word, and I definitely didn't mean to suggest
> that switches should no longer require `default:`).
>
>
> You misunderstand me.  This is the same problem as the one I brought up
> before: You are subject to the halting problem in the general case. The
> catch-all clause isn’t just noise, it’s a fundamental necessity to
> guarantee sound semantics for this particular class of switch statements
> that, according to a fundamental barrier in computer science, cannot be
> checked for exhaustiveness.
>
> On top of that, we cannot allow you to write an uncovered switch statement
> full of expression patterns because you would most-assuredly not handle all
> possible cases (suppose I extend your OptionSet with a new bit-pattern in a
> different module - your code will miscompile).
>
>
> On Sat, Nov 18, 2017 at 12:19 AM, Robert Widmann  > wrote:
>
>> Having spent a lot of time with ‘switch’, I don’t understand any of the
>> motivations or corresponding justifications for this idea.  Comments inline:
>>
>> ~Robert Widmann
>>
>> 2017/11/17 15:06、Peter Kamb via swift-evolution <
>> swift-evolution@swift.org>のメール:
>>
>> ## Title
>>
>> Add `match` statement as `switch`-like syntax alternative to `if case`
>> pattern matching
>>
>> ## Summary:
>>
>> 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`.
>>
>> However, the `switch` statement has several unique behaviors unrelated to
>> pattern matching. Namely:
>>
>>
>>
>>
>>  - Only the *first* matching case is executed. Subsequent matching cases
>> are not executed.
>>
>>
>> This is not unrelated to pattern matching, this is the expected behavior
>> of every 

Re: [swift-evolution] [Idea] [Pitch] Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching

2017-11-18 Thread Peter Kamb via swift-evolution
 > alternative to `fallthrough` that would continue matching cases

Yes, that would be interesting to look into. Do you have any references or
remember what the proposed keyword was called? Do any other languages have
this feature?



On Sat, Nov 18, 2017 at 10:53 AM Kevin Nattinger 
wrote:

> 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?
>
> > On Nov 17, 2017, at 12:06 PM, Peter Kamb via swift-evolution <
> swift-evolution@swift.org> wrote:
> >
> > ## Title
> >
> > Add `match` statement as `switch`-like syntax alternative to `if case`
> pattern matching
> >
> > ## Summary:
> >
> > 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`.
> >
> > However, the `switch` statement has several unique behaviors unrelated
> to pattern matching. Namely:
> >
> >  - Only the *first* matching case is executed. Subsequent matching cases
> are not executed.
> >  - `default:` case is required, even for expressions where a default
> case does not make sense.
> >
> > These behaviors prevent `switch` from being used as a generic
> match-patterns-against-a-single-expression statement.
> >
> > Swift should contain an equally-good pattern-matching statement that
> does not limit itself single-branch switching.
> >
> > ## Pitch:
> >
> > Add a `match` statement with the same elegant syntax as the `switch`
> statement, but without any of the "branch switching" baggage.
> >
> > ```
> > match someValue {
> > case patternOne:
> > always executed if pattern matches
> > case patternTwo:
> > always executed if pattern matches
> > }
> > ```
> >
> > The match statement would allow a single value to be filtered through
> *multiple* cases of pattern-matching evaluation.
> >
> > ## Example:
> >
> > ```
> > struct TextFlags: OptionSet {
> > let rawValue: Int
> > static let italics = TextFlags(rawValue: 1 << 1)
> > static let bold= TextFlags(rawValue: 1 << 2)
> > }
> >
> > let textFlags: TextFlags = [.italics, .bold]
> >
> >
> >
> > // SWITCH STATEMENT
> > switch textFlags {
> > case let x where x.contains(.italics):
> > print("italics")
> > case let x where x.contains(.bold):
> > print("bold")
> > default:
> > print("forced to include a default case")
> > }
> > // prints "italics"
> > // Does NOT print "bold", despite .bold being set.
> >
> >
> >
> > // MATCH STATEMENT
> > match textFlags {
> > case let x where x.contains(.italics):
> > print("italics")
> > case let x where x.contains(.bold):
> > print("bold")
> > }
> > // prints "italics"
> > // prints "bold"
> > ```
> >
> > ## Enum vs. OptionSet
> >
> > The basic difference between `switch` and `match` is the same conceptual
> difference between `Emum` and an `OptionSet` bitmask.
> >
> > `switch` is essentially designed for enums: switching to a single
> logical branch based on the single distinct case represented by the enum.
> >
> > `match` would be designed for OptionSet bitmasks and similar constructs.
> Executing behavior for *any and all* of the following cases and patterns
> that match.
> >
> > 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.
> >
> > ## Existing Alternatives
> >
> > `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.
> >
> > Example using a string of independent `if case` statements:
> >
> > ```
> > if case let x = textFlags, x.contains(.italics) {
> > print("italics")
> > }
> >
> > if case let x = textFlags, x.contains(.bold) {
> > print("bold")
> > }
> > ```
> >
> > ## `match` statement benefits:
> >
> >  - Allow filtering a single object through *multiple* cases of pattern
> matching, executing *all* cases that match.
> >
> >  - A syntax that exactly aligns with the familiar, succinct, elegant,
> and understandable `switch` syntax.
> >
> > - The keyword "match" highlights that pattern matching will occur. Would
> be even better than `switch` for initial introductions to pattern-matching.
> >
> >  - 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, 

Re: [swift-evolution] [Idea] [Pitch] Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching

2017-11-18 Thread Xiaodi Wu via swift-evolution
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.


On Sat, Nov 18, 2017 at 12:53 PM, Kevin Nattinger via swift-evolution <
swift-evolution@swift.org> wrote:

> 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?
>
> > On Nov 17, 2017, at 12:06 PM, Peter Kamb via swift-evolution <
> swift-evolution@swift.org> wrote:
> >
> > ## Title
> >
> > Add `match` statement as `switch`-like syntax alternative to `if case`
> pattern matching
> >
> > ## Summary:
> >
> > 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`.
> >
> > However, the `switch` statement has several unique behaviors unrelated
> to pattern matching. Namely:
> >
> >  - Only the *first* matching case is executed. Subsequent matching cases
> are not executed.
> >  - `default:` case is required, even for expressions where a default
> case does not make sense.
> >
> > These behaviors prevent `switch` from being used as a generic
> match-patterns-against-a-single-expression statement.
> >
> > Swift should contain an equally-good pattern-matching statement that
> does not limit itself single-branch switching.
> >
> > ## Pitch:
> >
> > Add a `match` statement with the same elegant syntax as the `switch`
> statement, but without any of the "branch switching" baggage.
> >
> > ```
> > match someValue {
> > case patternOne:
> > always executed if pattern matches
> > case patternTwo:
> > always executed if pattern matches
> > }
> > ```
> >
> > The match statement would allow a single value to be filtered through
> *multiple* cases of pattern-matching evaluation.
> >
> > ## Example:
> >
> > ```
> > struct TextFlags: OptionSet {
> > let rawValue: Int
> > static let italics = TextFlags(rawValue: 1 << 1)
> > static let bold= TextFlags(rawValue: 1 << 2)
> > }
> >
> > let textFlags: TextFlags = [.italics, .bold]
> >
> >
> >
> > // SWITCH STATEMENT
> > switch textFlags {
> > case let x where x.contains(.italics):
> > print("italics")
> > case let x where x.contains(.bold):
> > print("bold")
> > default:
> > print("forced to include a default case")
> > }
> > // prints "italics"
> > // Does NOT print "bold", despite .bold being set.
> >
> >
> >
> > // MATCH STATEMENT
> > match textFlags {
> > case let x where x.contains(.italics):
> > print("italics")
> > case let x where x.contains(.bold):
> > print("bold")
> > }
> > // prints "italics"
> > // prints "bold"
> > ```
> >
> > ## Enum vs. OptionSet
> >
> > The basic difference between `switch` and `match` is the same conceptual
> difference between `Emum` and an `OptionSet` bitmask.
> >
> > `switch` is essentially designed for enums: switching to a single
> logical branch based on the single distinct case represented by the enum.
> >
> > `match` would be designed for OptionSet bitmasks and similar constructs.
> Executing behavior for *any and all* of the following cases and patterns
> that match.
> >
> > 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.
> >
> > ## Existing Alternatives
> >
> > `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.
> >
> > Example using a string of independent `if case` statements:
> >
> > ```
> > if case let x = textFlags, x.contains(.italics) {
> > print("italics")
> > }
> >
> > if case let x = textFlags, x.contains(.bold) {
> > print("bold")
> > }
> > ```
> >
> > ## `match` statement benefits:
> >
> >  - Allow filtering a single object through *multiple* cases of pattern
> matching, executing *all* cases that match.
> >
> >  - A syntax that exactly aligns with the familiar, succinct, elegant,
> and understandable `switch` syntax.
> >
> > - The keyword "match" highlights that pattern matching will occur. Would
> be even better than `switch` for initial introductions to pattern-matching.
> >
> >  - 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, 

Re: [swift-evolution] [Idea] [Pitch] Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching

2017-11-18 Thread Kevin Nattinger via swift-evolution
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?

> On Nov 17, 2017, at 12:06 PM, Peter Kamb via swift-evolution 
>  wrote:
> 
> ## Title
> 
> Add `match` statement as `switch`-like syntax alternative to `if case` 
> pattern matching
> 
> ## Summary:
> 
> 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`.
> 
> However, the `switch` statement has several unique behaviors unrelated to 
> pattern matching. Namely:  
> 
>  - Only the *first* matching case is executed. Subsequent matching cases are 
> not executed.
>  - `default:` case is required, even for expressions where a default case 
> does not make sense.
> 
> These behaviors prevent `switch` from being used as a generic 
> match-patterns-against-a-single-expression statement.
> 
> Swift should contain an equally-good pattern-matching statement that does not 
> limit itself single-branch switching.
> 
> ## Pitch:
> 
> Add a `match` statement with the same elegant syntax as the `switch` 
> statement, but without any of the "branch switching" baggage.
> 
> ```
> match someValue {
> case patternOne:
> always executed if pattern matches
> case patternTwo:
> always executed if pattern matches
> }
> ```
> 
> The match statement would allow a single value to be filtered through 
> *multiple* cases of pattern-matching evaluation.
> 
> ## Example:
> 
> ```
> struct TextFlags: OptionSet {
> let rawValue: Int
> static let italics = TextFlags(rawValue: 1 << 1)
> static let bold= TextFlags(rawValue: 1 << 2)
> }
> 
> let textFlags: TextFlags = [.italics, .bold]
> 
> 
> 
> // SWITCH STATEMENT
> switch textFlags {
> case let x where x.contains(.italics):
> print("italics")
> case let x where x.contains(.bold):
> print("bold")
> default:
> print("forced to include a default case")
> }
> // prints "italics"
> // Does NOT print "bold", despite .bold being set.
> 
> 
> 
> // MATCH STATEMENT
> match textFlags {
> case let x where x.contains(.italics):
> print("italics")
> case let x where x.contains(.bold):
> print("bold")
> }
> // prints "italics"
> // prints "bold"
> ```
> 
> ## Enum vs. OptionSet
> 
> The basic difference between `switch` and `match` is the same conceptual 
> difference between `Emum` and an `OptionSet` bitmask.
> 
> `switch` is essentially designed for enums: switching to a single logical 
> branch based on the single distinct case represented by the enum.
> 
> `match` would be designed for OptionSet bitmasks and similar constructs. 
> Executing behavior for *any and all* of the following cases and patterns that 
> match.
> 
> 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.
> 
> ## Existing Alternatives
> 
> `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.
> 
> Example using a string of independent `if case` statements:
> 
> ```
> if case let x = textFlags, x.contains(.italics) {
> print("italics")
> }
> 
> if case let x = textFlags, x.contains(.bold) {
> print("bold")
> }
> ```
> 
> ## `match` statement benefits:
> 
>  - Allow filtering a single object through *multiple* cases of pattern 
> matching, executing *all* cases that match.
> 
>  - A syntax that exactly aligns with the familiar, succinct, elegant, and 
> understandable `switch` syntax.
> 
> - The keyword "match" highlights that pattern matching will occur. Would be 
> even better than `switch` for initial introductions to pattern-matching.
> 
>  - 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) {`
> 
>  - 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."
> 
>  - A single `match controlExpression` at the top rather than 
> `controlExpression` being repeated (and possibly changed) in every single `if 
> case` statement.
> 
>  - Duplicated `controlExpression` is an opportunity 

Re: [swift-evolution] [Idea] [Pitch] Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching

2017-11-18 Thread Robert Widmann via swift-evolution

> On Nov 18, 2017, at 4:42 AM, Peter Kamb  wrote:
> 
> Thanks for the review.
> 
> Motivation is essentially to provide a low-friction way to write `if case / 
> if case / if case` statements that are all matching on the same expression, 
> much in the same way that `switch` offers a way to write `if / else if / 
> else` statements against the same expression.

That much I understand, but there’s no motivation as to why this kind of change 
needs to occur over a line of if-case statements.  There isn’t anything in the 
pitch that demonstrates friction outside of a personal distaste for the syntax.

> 
>  > > Only the *first* matching case is executed. Subsequent matching cases 
> are not executed.
>  > This is not unrelated to pattern matching, this is the expected behavior 
> of every pattern matching algorithm.
>  
> By that I meant that a string of `if case / if case / if case` statements, 
> each individually matching against the same expression, would match and 
> execute all 3 cases. A `switch` using the same 3 cases would execute only the 
> first matching case. I took the "only first case" behavior to be a property 
> of the *switch*, not of "pattern matching" in general? But perhaps I'm using 
> the wrong terminology.
>  
>  > > Allow filtering a single object through *multiple* cases of pattern 
> matching, executing *all* cases that match.
>  > Again, this is not pattern matching.
> 
> A switch can have multiple cases that "would" match and execute, if you were 
> to comment out the successful case above them. Why would a hypothetical 
> statement that executed *all* matching cases, rather than just the first 
> case, not be pattern matching? It wouldn't be a `switch`, for sure, but it 
> seems like it would be just as much "pattern matching" as an `if-case`.

It is not pattern matching because it violates minimality - and we have a 
warning today when you write switches of overlapping patterns.  Consider the 
semantics of pattern matching in the context of a tree - but it just so happens 
the nodes of this tree are your patterns and the branches, actual branches 
where control flow splits along the case-matrix.  The goal is not to execute a 
find-all, the goal is to execute a find - exactly like a regular expression.  

> 
>  > This is not boilerplate!
> 
> An `if case / if case` pair would not have the same concept of a shared 
> `else/default` case used by a `switch` or `if/else`, which is why I said it 
> would not be needed. `if-case` does not require an else/default. (Perhaps 
> boilerplate was the wrong word, and I definitely didn't mean to suggest that 
> switches should no longer require `default:`).

You misunderstand me.  This is the same problem as the one I brought up before: 
You are subject to the halting problem in the general case. The catch-all 
clause isn’t just noise, it’s a fundamental necessity to guarantee sound 
semantics for this particular class of switch statements that, according to a 
fundamental barrier in computer science, cannot be checked for exhaustiveness.

On top of that, we cannot allow you to write an uncovered switch statement full 
of expression patterns because you would most-assuredly not handle all possible 
cases (suppose I extend your OptionSet with a new bit-pattern in a different 
module - your code will miscompile).

> 
> On Sat, Nov 18, 2017 at 12:19 AM, Robert Widmann  > wrote:
> Having spent a lot of time with ‘switch’, I don’t understand any of the 
> motivations or corresponding justifications for this idea.  Comments inline:
> 
> ~Robert Widmann 
> 
> 2017/11/17 15:06、Peter Kamb via swift-evolution  >のメール:
> 
>> ## Title
>> 
>> Add `match` statement as `switch`-like syntax alternative to `if case` 
>> pattern matching
>> 
>> ## Summary:
>> 
>> 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`.
>> 
>> However, the `switch` statement has several unique behaviors unrelated to 
>> pattern matching. Namely:  
> 
> 
>> 
>>  - Only the *first* matching case is executed. Subsequent matching cases are 
>> not executed.
> 
> This is not unrelated to pattern matching, this is the expected behavior of 
> every pattern matching algorithm.  The intent is to compile a switch-case 
> tree down to (conceptually) a (hopefully minimal) if-else tree.  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.
> 
>>  - `default:` case is required, even for expressions where a default case 
>> does not make sense.
> 
> Expression patterns may look to be covered “at first glance”, but the 

Re: [swift-evolution] [Pitch] Introduce User-defined "Dynamic Member Lookup" Types

2017-11-18 Thread Paul Cantrell via swift-evolution

> On Nov 18, 2017, at 1:44 AM, Jean-Daniel  wrote:
> 
> 
>> …
>> 
>> In Ruby, `myObj.name()` is equivalent to `myObj.name`, and either works. In 
>> Swift, I don’t see that it’s possible to make both work with Chris’s 
>> proposal.
> 
> IIUC, the goal is not to make swift look and behave the same as ruby or 
> python, but to be able to use ruby or python object in a swift way (without 
> indirect call and other nasty constructions). I don’t see requiring the 
> .property syntax and prohibiting the .property() one as an issue. I would 
> even say this is the thing to do, as it would make the swift code more 
> understandable to Swift dev that are not used to Ruby.


It really wouldn’t. Zero-arg Ruby methods are a mixture of property-like things 
that would certainly not use parens in Swift, and function-like things that 
certainly would:

// Idiomatic Swift:
post.author.name.reversed()

// Swift bridging to Ruby…

// …if no-args methods •must• use parens:
post.author().name().reverse()

// …if no-args methods •can’t• use parens:
post.author.name.reverse

If the goal is to make Swift mostly look like Swift even when bridging to Ruby, 
then the bridge needs to support both access forms.

Cheers, P

___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] [Idea] [Pitch] Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching

2017-11-18 Thread Tino Heth via swift-evolution
I haven’t spend that much time on thinking about the implications, but at first 
sight, it sounds like a sensible idea — maybe even more useful than switch 
(whose behavior could be mimicked).
But switch is established, and I don’t see a realistic chance to either replace 
it or add a construct that is quite similar.
Afaics, it will already be hard to collect feedback, so it might be a good idea 
to delay the pitch until the move to Discourse is done.
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] [Idea] [Pitch] Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching

2017-11-18 Thread Peter Kamb via swift-evolution
Thanks for the review.

Motivation is essentially to provide a low-friction way to write `if case /
if case / if case` statements that are all matching on the same expression,
much in the same way that `switch` offers a way to write `if / else if /
else` statements against the same expression.

 > > Only the *first* matching case is executed. Subsequent matching cases
are not executed.
 > This is not unrelated to pattern matching, this is the expected behavior
of every pattern matching algorithm.

By that I meant that a string of `if case / if case / if case` statements,
each individually matching against the same expression, would match and
execute all 3 cases. A `switch` using the same 3 cases would execute only
the first matching case. I took the "only first case" behavior to be a
property of the *switch*, not of "pattern matching" in general? But perhaps
I'm using the wrong terminology.

 > > Allow filtering a single object through *multiple* cases of pattern
matching, executing *all* cases that match.
 > Again, this is not pattern matching.

A switch can have multiple cases that "would" match and execute, if you
were to comment out the successful case above them. Why would a
hypothetical statement that executed *all* matching cases, rather than just
the first case, not be pattern matching? It wouldn't be a `switch`, for
sure, but it seems like it would be just as much "pattern matching" as an
`if-case`.

 > This is not boilerplate!

An `if case / if case` pair would not have the same concept of a shared
`else/default` case used by a `switch` or `if/else`, which is why I said it
would not be needed. `if-case` does not require an else/default. (Perhaps
boilerplate was the wrong word, and I definitely didn't mean to suggest
that switches should no longer require `default:`).

 > A syntax that duplicates the existing if-case construct

Right, it would duplicate `if-case` just as `switch` duplicates `if-else`.

Cheers, thanks.

Peter

On Sat, Nov 18, 2017 at 12:19 AM, Robert Widmann 
wrote:

> Having spent a lot of time with ‘switch’, I don’t understand any of the
> motivations or corresponding justifications for this idea.  Comments inline:
>
> ~Robert Widmann
>
> 2017/11/17 15:06、Peter Kamb via swift-evolution  >のメール:
>
> ## Title
>
> Add `match` statement as `switch`-like syntax alternative to `if case`
> pattern matching
>
> ## Summary:
>
> 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`.
>
> However, the `switch` statement has several unique behaviors unrelated to
> pattern matching. Namely:
>
>
>
>
>  - Only the *first* matching case is executed. Subsequent matching cases
> are not executed.
>
>
> This is not unrelated to pattern matching, this is the expected behavior
> of every pattern matching algorithm.  The intent is to compile a
> switch-case tree down to (conceptually) a (hopefully minimal) if-else
> tree.  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.
>
>  - `default:` case is required, even for expressions where a default case
> does not make sense.
>
>
> 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.  Further, your proposed idea has absolutely
> nothing to do with this.
>
>
> These behaviors prevent `switch` from being used as a generic
> match-patterns-against-a-single-expression statement.
>
> Swift should contain an equally-good pattern-matching statement that does
> not limit itself single-branch switching.
>
> ## Pitch:
>
> Add a `match` statement with the same elegant syntax as the `switch`
> statement, but without any of the "branch switching" baggage.
>
> ```
> match someValue {
> case patternOne:
> always executed if pattern matches
> case patternTwo:
> always executed if pattern matches
> }
> ```
>
> The match statement would allow a single value to be filtered through
> *multiple* cases of pattern-matching evaluation.
>
> ## Example:
>
> ```
> struct TextFlags: OptionSet {
> let rawValue: Int
> static let italics = TextFlags(rawValue: 1 << 1)
> static let bold= TextFlags(rawValue: 1 << 2)
> }
>
> let textFlags: TextFlags = [.italics, .bold]
>
>
>
> // SWITCH STATEMENT
> switch textFlags {
> case let x where x.contains(.italics):
> print("italics")
> case let x where x.contains(.bold):
> print("bold")
> default:
> print("forced to include a default case")
> }
> // prints "italics"
> // Does NOT print "bold", despite .bold being set.
>
>
>
> // MATCH STATEMENT
> match textFlags {
> case let x where x.contains(.italics):
> 

Re: [swift-evolution] [Idea] [Pitch] Add `match` statement as `switch`-like syntax alternative to `if case` pattern matching

2017-11-18 Thread Robert Widmann via swift-evolution
Having spent a lot of time with ‘switch’, I don’t understand any of the 
motivations or corresponding justifications for this idea.  Comments inline:

~Robert Widmann 

2017/11/17 15:06、Peter Kamb via swift-evolution のメール:

> ## Title
> 
> Add `match` statement as `switch`-like syntax alternative to `if case` 
> pattern matching
> 
> ## Summary:
> 
> 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`.
> 
> However, the `switch` statement has several unique behaviors unrelated to 
> pattern matching. Namely:  


> 
>  - Only the *first* matching case is executed. Subsequent matching cases are 
> not executed.

This is not unrelated to pattern matching, this is the expected behavior of 
every pattern matching algorithm.  The intent is to compile a switch-case tree 
down to (conceptually) a (hopefully minimal) if-else tree.  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.

>  - `default:` case is required, even for expressions where a default case 
> does not make sense.

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.  Further, your proposed idea has absolutely nothing to do with 
this.

> 
> These behaviors prevent `switch` from being used as a generic 
> match-patterns-against-a-single-expression statement.
> 
> Swift should contain an equally-good pattern-matching statement that does not 
> limit itself single-branch switching.
> 
> ## Pitch:
> 
> Add a `match` statement with the same elegant syntax as the `switch` 
> statement, but without any of the "branch switching" baggage.
> 
> ```
> match someValue {
> case patternOne:
> always executed if pattern matches
> case patternTwo:
> always executed if pattern matches
> }
> ```
> 
> The match statement would allow a single value to be filtered through 
> *multiple* cases of pattern-matching evaluation.
> 
> ## Example:
> 
> ```
> struct TextFlags: OptionSet {
> let rawValue: Int
> static let italics = TextFlags(rawValue: 1 << 1)
> static let bold= TextFlags(rawValue: 1 << 2)
> }
> 
> let textFlags: TextFlags = [.italics, .bold]
> 
> 
> 
> // SWITCH STATEMENT
> switch textFlags {
> case let x where x.contains(.italics):
> print("italics")
> case let x where x.contains(.bold):
> print("bold")
> default:
> print("forced to include a default case")
> }
> // prints "italics"
> // Does NOT print "bold", despite .bold being set.
> 
> 
> 
> // MATCH STATEMENT
> match textFlags {
> case let x where x.contains(.italics):
> print("italics")
> case let x where x.contains(.bold):
> print("bold")
> }
> // prints "italics"
> // prints "bold"
> ```
> 
> ## Enum vs. OptionSet
> 
> The basic difference between `switch` and `match` is the same conceptual 
> difference between `Emum` and an `OptionSet` bitmask.
> 
> `switch` is essentially designed for enums: switching to a single logical 
> branch based on the single distinct case represented by the enum.
> 
> `match` would be designed for OptionSet bitmasks and similar constructs. 
> Executing behavior for *any and all* of the following cases and patterns that 
> match.
> 
> 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.
> 
> ## Existing Alternatives
> 
> `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.
> 
> Example using a string of independent `if case` statements:
> 
> ```
> if case let x = textFlags, x.contains(.italics) {
> print("italics")
> }
> 
> if case let x = textFlags, x.contains(.bold) {
> print("bold")
> }
> ```
> 
> ## `match` statement benefits:
> 
>  - Allow filtering a single object through *multiple* cases of pattern 
> matching, executing *all* cases that match.

Again, this is not pattern matching.

> 
>  - A syntax that exactly aligns with the familiar, succinct, elegant, and 
> understandable `switch` syntax.

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.  Even less, considering you unwrap in the if-case to 
exaggerate the