question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Joint Case Clauses in Switch Statements

See original GitHub issue

I assume that you left out case fallthrough in the switch statement by design. Indeed, fallthrough can be tricky for newcomers to the language. On the other hand, switch feels slightly crippled without it.

Here is an example of the current behaviour:

switch command
    case "-lvlup" // Currently doesn't do anything. I'm used to fallthrough so this came as a surpise for me.
    case "-lu"
        lvlup()

I’d like to execute the same statement for both case clauses without repeating the statement. This is currently not possible.

I propose the following solution: Joint case clauses. E.g.,

switch command
    case "-lvlup", "-lu"
        lvlup()

It’s an alternative approach to fallthrough without the ambiguity. I’ve used the a comma symbol to separate the literals in the case clause. If you prefer another symbol then just use your preference.

Assuming that switch compiles to a long if..elseif..else statement, the implementation can simply or together the arguments of the joint case clause.

What do you think? Is it worth the effort?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
peqcommented, Mar 16, 2019

Several cases can now be combined using a pipe symbol (|).

enum Animal
    Sheep
    Dog
    Eagle

function canFly(Animal animal) returns boolean
    switch animal
        case Dog | Sheep
            return false
        case Eagle
            return true

I used the pipe rather than a comma, because a comma is often used for things like tuples and function parameters and we might add language features to match on tuples in the future.

1reaction
Frottycommented, Feb 23, 2019

Yes, switch was implemented in the most basic manner with the only additional thing being that it checks if all cases of an enum are handled. I think improvements could be made, perhaps even syntax wise similar to Kotlin’s when.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using two values for one switch case statement - Stack Overflow
You can use have both CASE statements as follows. ... FALLTHROUGH: Another point of interest is the break statement. Each break statement terminates...
Read more >
Selection statements - if , else and switch - Microsoft Learn
The if statement selects a statement to execute based on the value of a Boolean expression. An if statement can be combined with...
Read more >
Examples of switch statements - IBM
The following switch statement contains several case clauses and one default clause. Each clause contains a function call and a break statement.
Read more >
Conditional Statements: If, Else, and Switch - KIRUPA
As we can see, if / else statements are very similar to switch statements and vice versa. The default case block becomes an...
Read more >
"switch" statements should have at least 3 "case" clauses
"switch" statements should have at least 3 "case" clauses ... switch statements are useful when there are many different cases depending on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found