About "default" keyword in switch statement


(星野恵瑠) #1

Many languages have a switch statement and most (maybe all?) of them use "default" keyword for the condition which doesn't match any of those listed in the switch statement.

But sometimes that condition is just an exception, which literally IS NOT a default condition.

So how about using other keywords like "else" instead of "default"?


(星野恵瑠) #2

p.s.

I've read this mail and totally understand that Swift is designed to be familiar with C family languages, which "default" is widely used.
https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md

But since Swift 3.0 has already make many source breaking changes and supports many features which most C family languages don't have, like labels in functions (except Objective-C, I guess), "inout" keyword in functions, etc, and one of Swift's goals is to make syntax "clear," hope we can re-consider the "default" keyword.

Thanks for your time.

···

2016/12/06 12:48、星野恵瑠 <el.hoshino@me.com> のメール:

Many languages have a switch statement and most (maybe all?) of them use "default" keyword for the condition which doesn't match any of those listed in the switch statement.

But sometimes that condition is just an exception, which literally IS NOT a default condition.

So how about using other keywords like "else" instead of "default"?


(Robert Widmann) #3

Perhaps you're thinking about this choice of syntax from a different angle than was intended. `default` is not literally meant to be a "default value" or, in most cases, "the default case", rather a "default pattern"; a place for control to flow given no other option. Some other languages, especially those that emphasize patterns, syntaxize words like "otherwise" or simply use "_", but I've not seen languages that mix C-style switch-case keywords with C-style branching keywords before.

~Robert Widmann

2016/12/05 22:48、星野恵瑠 via swift-evolution <swift-evolution@swift.org> のメッセージ:

···

Many languages have a switch statement and most (maybe all?) of them use "default" keyword for the condition which doesn't match any of those listed in the switch statement.

But sometimes that condition is just an exception, which literally IS NOT a default condition.

So how about using other keywords like "else" instead of "default"?
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Joe Groff) #4

"Case Else" is common in Pascal-lineage syntaxes. (Shameless plug) We also used 'else' for the default block signifier in Clay, since we adopted block syntax for cases instead of label-style cases as in C, e.g. 'switch (x) case (1) { ... } else { ... }'.

-Joe

···

On Dec 6, 2016, at 3:48 PM, Robert Widmann via swift-evolution <swift-evolution@swift.org> wrote:

Perhaps you're thinking about this choice of syntax from a different angle than was intended. `default` is not literally meant to be a "default value" or, in most cases, "the default case", rather a "default pattern"; a place for control to flow given no other option. Some other languages, especially those that emphasize patterns, syntaxize words like "otherwise" or simply use "_", but I've not seen languages that mix C-style switch-case keywords with C-style branching keywords before.


(Jeremy Pereira) #5

p.s.

I've read this mail and totally understand that Swift is designed to be familiar with C family languages, which "default" is widely used.
https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md

Did you read the first paragraph of that page and follow its advice to "familiarize yourself with the discussions that we have already had”? They were heated and protracted and ended with the situation unchanged.

Personally, I don’t see the problem with default. I really don’t think the breakage of pretty much everything caused by removing it is worth it even if there are better synonyms.

Plus, I little while ago, I came across a situation where the code would have read marginally better if I had been able to put the default case at the top of the switch (illegal in Swift 3) with the other cases below it (illegal in Swift 3). If that restriction were ever to be lifted, “else” would suddenly be a very poor choice.

···

On 6 Dec 2016, at 04:11, 星野恵瑠 via swift-evolution <swift-evolution@swift.org> wrote:

But since Swift 3.0 has already make many source breaking changes and supports many features which most C family languages don't have, like labels in functions (except Objective-C, I guess), "inout" keyword in functions, etc, and one of Swift's goals is to make syntax "clear," hope we can re-consider the "default" keyword.

Thanks for your time.

2016/12/06 12:48、星野恵瑠 <el.hoshino@me.com> のメール:

Many languages have a switch statement and most (maybe all?) of them use "default" keyword for the condition which doesn't match any of those listed in the switch statement.

But sometimes that condition is just an exception, which literally IS NOT a default condition.

So how about using other keywords like "else" instead of "default"?

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


(星野恵瑠) #6

Hi everyone

Thanks for all of your replies, and also thanks a lot for your time discussing this topic with me.
Also especially thanks to Robert to explain the meaning of "default" in switch statement. Now I can agree keeping using "default" keyword in switch statement.
Sorry to bother you all with this topic.

Actually before I send that mail I talked about this keyword with some of my Swift developer friends, and some of them told me that they have the same trouble in using default in switch statement, while others told me that they've never thought about it because it's just what they did in C/C++ development.
So thanks to your advices, now I can explain it to my friends.

···

2016/12/07 8:48、Robert Widmann <devteam.codafi@gmail.com> のメール:

Perhaps you're thinking about this choice of syntax from a different angle than was intended. `default` is not literally meant to be a "default value" or, in most cases, "the default case", rather a "default pattern"; a place for control to flow given no other option. Some other languages, especially those that emphasize patterns, syntaxize words like "otherwise" or simply use "_", but I've not seen languages that mix C-style switch-case keywords with C-style branching keywords before.

~Robert Widmann

2016/12/05 22:48、星野恵瑠 via swift-evolution <swift-evolution@swift.org> のメッセージ:

Many languages have a switch statement and most (maybe all?) of them use "default" keyword for the condition which doesn't match any of those listed in the switch statement.

But sometimes that condition is just an exception, which literally IS NOT a default condition.

So how about using other keywords like "else" instead of "default"?
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(David Sweeris) #7

+1

···

On Dec 6, 2016, at 9:51 AM, Jeremy Pereira via swift-evolution <swift-evolution@swift.org> wrote:

Plus, I little while ago, I came across a situation where the code would have read marginally better if I had been able to put the default case at the top of the switch (illegal in Swift 3) with the other cases below it (illegal in Swift 3).