Skip to content
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

switch-case-default over switch-when-else #785

Closed
satyr opened this issue Oct 21, 2010 · 17 comments
Closed

switch-case-default over switch-when-else #785

satyr opened this issue Oct 21, 2010 · 17 comments

Comments

@satyr
Copy link
Collaborator

satyr commented Oct 21, 2010

Before

switch a
  when b
    foo()
  when c, d
    bar()
  else
    baz()

After

switch a
case b
  foo()
case c, d
  bar()
default
  baz()
@StanAngeloff
Copy link
Contributor

Oh bummer, there was another ticket for this which Jeremy closed. I'll try and dig it up.

EDIT: #697.

@satyr
Copy link
Collaborator Author

satyr commented Oct 22, 2010

On a related note, how about changing the subjectless compilation:

switch (true) {
  case !!a:
    b;
    break;
}

into:

switch (false) {
  case !a:
    b;
    break;
}

@TrevorBurnham
Copy link
Collaborator

This makes sense to me. What was the original justification for using when-else instead of JavaScript's case-default?

@zmthy
Copy link
Collaborator

zmthy commented Oct 23, 2010

Because it matches the when of loops and else of if.

@TrevorBurnham
Copy link
Collaborator

OK, on further reflection, I like the way it is now. Reasons:

  1. The when-else syntax is consistent with Ruby. Also as in Ruby, there's no fall-through.
  2. Because there's no fall-through, a switch statement from JavaScript may not be easily ported to CoffeeScript. The different keywords help to signal this (and preserve sanity if you're alternately working on CoffeeScript and JavaScript code).
  3. As to the indentation, it's true that the standard style in Ruby is for case and when to line up, but it feels more consistent with CoffeeScript to keep it: If you were to remove the switch a line, you would get a syntax error because the case and default lines are dependent on it; the indentation clearly signals this.

@satyr
Copy link
Collaborator Author

satyr commented Oct 23, 2010

  1. CoffeeScript isnt Ruby

  2. for is also quite different but we keep the keyword.

  3. Would you rather write:

     try
       catch
       finally #?
    

@zmthy
Copy link
Collaborator

zmthy commented Oct 23, 2010

Coffeescript isn't Javascript, either, at least not in syntax. The switch doesn't work the same as in Javascript, so the syntax doesn't require consistency.

@TrevorBurnham
Copy link
Collaborator

OK, #3 is a good point. I'm fine with either keeping the mandatory indentation or adopting the Ruby standard, as long as there's a standard. The important thing is that we don't wind up in a situation where indentation is optional and half of folks do it one way and half do it another way.

@StanAngeloff
Copy link
Contributor

The switch doesn't work the same as in Javascript, so the syntax doesn't require consistency.

This isn't true as of 0.9.3.

switch cond
  when true then false
  when false then true

into

switch (cond) {
  case true:
    false;
    break;
  case false:
    true;
    break;
}

No more ifs.

I prefer to keep the whens indented as they follow the switch. Perhaps the else can be placed one level out:

switch a
  when b
    foo()
  when c, d
    bar()
else
  baz()

@TrevorBurnham
Copy link
Collaborator

That looks like the worst of both worlds to me... if there's an else clause, it should be at the same level of indentation as the case clauses.

@zmthy
Copy link
Collaborator

zmthy commented Oct 24, 2010

This isn't true as of 0.9.3.

I was referring to the fact that we insert break into each case.

@jashkenas
Copy link
Owner

Let's leave it the way it is. I see the appeal of mirroring JS syntax, but it's not as compelling as it could be, because case and default are not used anywhere else in JavaScript. The general readability of switch ... when, the semantic consistency between the use of when and else here and elsewhere in CoffeeScript -- all tip the scales in favor of the current style.

@satyr
Copy link
Collaborator Author

satyr commented Oct 24, 2010

the semantic consistency between the use of when

I don't think so--this when can take multiple condition, while others can't. And the ambiguity issue holds. We shouldn't use when/else there.

@satyr
Copy link
Collaborator Author

satyr commented Oct 24, 2010

I guess I can use this indentation for now:

switch a
 when b, c
  while foo()
    bar()
 else
  baz()

@JhonnyJason
Copy link

Does anybody have an Idea why this does not work?

switch state
    when "stateA"
        console.log "we are at state stateA"
        stateIndicator.innerHTML = "stateA"
    when "stateB"
        console.log "we are at state stateB"
        stateIndicator.innerHTML = "stateB"
    else
        console.log "we had a weird state: " + state
        stateIndicator.innerHTML = "weirdState"

It gives me an Error for the line having the else:

error: indentation mismatch

As I would say the error message is not true^^... I know they usually are true but still I need to understand why - from this thread it appears to be the right indention - as my logic also would support

@GeoffreyBooth
Copy link
Collaborator

It works fine. Generally “indentation mismatch” means you’re mixing tabs and spaces.

@JhonnyJason
Copy link

OK got it thnx :-)

Beware of trailing spaces!!

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

No branches or pull requests

7 participants