ceylon / ceylon-spec

DEPRECATED
Apache License 2.0
108 stars 34 forks source link

allow "else if" in "switch" statements #1451

Closed gavinking closed 8 years ago

gavinking commented 8 years ago

While working on the IDE, I noticed that it's really inconvenient to not be able to write:

switch (maybeFooOrBar)
case (is Foo) { … }
case (is Bar { … }
else if (something) { … }

Previously the language forced me to write:

switch (maybeFooOrBar)
case (is Foo) { … }
case (is Bar { … }
else {
    if (something) { … }
}

This is a bit irregular, since in at most other contexts, you can write else if where else is supported.

So I made an adjustment to the parser to support this.

I had a tiny doubt about this because it seems like this defeats the "exhaustiveness" of the switch, but I don't think that objection is correct, since:

  1. The rule is that when you write an else, you're indicating that you know the switch is inexhaustive ... and this complies with that.
  2. This objection confuses exhaustiveness analysis with definite return/assignment checking, which is actually a different thing, and which is not at all broken by this change.

If anyone objects to this change, please reopen this issue.