-
Notifications
You must be signed in to change notification settings - Fork 2k
Documentation for switch statements with no control expression #2417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation for switch statements with no control expression #2417
Conversation
While legal, I think this would usually be an abuse of |
+1. A 'switch' statement is very descriptive: from the get go you know that at most one of many conditions will take place. I find it more expressive and harder to get confused. There's also only one way to indent it, compared to nested else-if that could be indented in different ways. Other argument: what else could/should @gabehollombe 's code be translated to? Finally: it works now, so for compatibility... it should be supported in the future too. |
I feel the switch format reads a lot nicer. Less keywords (if/else) on the On Fri, Jul 6, 2012 at 9:36 PM, Jeremy Ashkenas <
|
PS: Forgot to mention that Ruby supports this style of |
+1 this is cleaner and more convenient way than 'else if' chains. Less noise, more sense. Pls include ;) 'Least surprising' principle, as in ruby, where this construction is widely used (while ruby hardly misses chained comaprisons ;)). |
+1 for documenting empty score = 76
grade = switch
when score < 60 then 'F'
when score < 70 then 'D'
when score < 80 then 'C'
when score < 90 then 'B'
else 'A' It can also be written as a cascade of |
@epidemian That does look a lot clearer than the initial proposal by @gabehollombe. Shows the common usage for ranges very nicely. @gabehollombe Any objections to using @epidemian's approach instead? |
No objections at all. =-) On Sun, Oct 7, 2012 at 5:33 AM, Justin Clift [email protected]:
|
@gabehollombe Heh, cool. Any chance you can update the pull request? :) |
…trol_expression Documentation for switch statements with no control expression
👏 |
Added the following to the docs in the Switch section:
Switch statements can also be used without a control expression, turning them in to a cleaner alternative to if/else chains.
score = 76
grade = switch
when score < 60 then 'F'
when score < 70 then 'D'
when score < 80 then 'C'
when score < 90 then 'B'
else 'A'
grade == 'C'