Skip to content

Switch statment with empty control expression defaults to false #2416

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

Closed
OnesimusUnbound opened this issue Jul 3, 2012 · 7 comments
Closed
Labels

Comments

@OnesimusUnbound
Copy link

# CS
switch 
   when "A" then console.log "Hi"
   else console.log "Bye"

// Generated JS
switch (false) {  
  case !"A":    
    console.log("Hi");    
    break;  
  default:    
    console.log("Bye");
 }

CS should report this as an invalid syntax.

Also, I find it strange that ! is prepend for all values in case statament in generated JS.

sample

@gkz
Copy link

gkz commented Jul 3, 2012

This is on purpose and useful. You are switching on true - a nice replacement for a series of if-else statements. It compiles to switching on false as it is slightly shorter to cast each of the cases to a boolean with a single ! rather than !!.

@OnesimusUnbound
Copy link
Author

@gkz Hmm, I see.

[enable, alerted] = [off, on]
switch 
  when enable then alert "Enabled"
  when alerted then alert "Altered"
  else alert "Normal"

However, don't see ths documented on the official site.

@michaelficarra
Copy link
Collaborator

It's intentional and undocumented. There's already an issue open that mentions that this needs to be documented. See #1808. @gabehollombe volunteered to improve the documentation.

@OnesimusUnbound
Copy link
Author

I was thinking of a good case of empty switch as part of documenting this feature and I think that it's does better in conditional assignment of value, which improves redability due to use of indent, unlike in if else.

# using switch
score = 90
grade = switch
   when score >= 97 then "A+"
   when score >= 94 then "A"
   when score >= 90 then "A-"
   when score >= 87 then "B+"
   when score >= 84 then "B"
   when score >= 80 then "B-"
   when score >= 78 then "C"
   when score >= 75 then "D"
   else                  "F"

alert grade

# using if else
score = 90
grade = if score >= 97 then "A+"
else if score >= 94 then "A"
else if score >= 90 then "A-"
else if score >= 87 then "B+"
else if score >= 84 then "B"
else if score >= 80 then "B-"
else if score >= 78 then "C"
else if score >= 75 then "D"
else                  "F"

alert grade

@gabehollombe
Copy link
Contributor

Yep, that's a great example. Do you want me to just go with that, and
polish it a bit with some context and wordsmithing?

On Wed, Jul 4, 2012 at 10:56 AM, OnesimusUnbound <
[email protected]

wrote:

I was thinking of a good case of empty switch as part of documenting this
feature and I think that it's does better in conditional assignment of
value, which improves redability due to use of indent, unlike in if else.

# using switch
score = 90
grade = switch
   when score >= 97 then "A+"
   when score >= 94 then "A"
   when score >= 90 then "A-"
   when score >= 87 then "B+"
   when score >= 84 then "B"
   when score >= 80 then "B-"
   when score >= 78 then "C"
   when score >= 75 then "D"
   else                  "F"

alert grade

# using if else
score = 90
grade = if score >= 97 then "A+"
else if score >= 94 then "A"
else if score >= 90 then "A-"
else if score >= 87 then "B+"
else if score >= 84 then "B"
else if score >= 80 then "B-"
else if score >= 78 then "C"
else if score >= 75 then "D"
else                  "F"

alert grade

Reply to this email directly or view it on GitHub:
#2416 (comment)

@OnesimusUnbound
Copy link
Author

@gabehollombe sure, go ahead ;-)

@gabehollombe
Copy link
Contributor

Pull request is in: #2417

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants