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 Statement Fall Thru #93

Open
DavePearce opened this issue Jun 2, 2021 · 3 comments
Open

Switch Statement Fall Thru #93

DavePearce opened this issue Jun 2, 2021 · 3 comments

Comments

@DavePearce
Copy link
Member

Suggestion: Require all switch cases are terminated with either break, continue or return.

@lojikil
Copy link

lojikil commented Jun 2, 2021

Do switch statement's cases currently fall through? it doesn't look that way, but as long as we keep that enforcement I think this wouldn't be terrible (although I'm a fan of not having to require a break for each case if we know that we don't fall through). If it does fall through, I think that's a huge win.

@DavePearce
Copy link
Member Author

DavePearce commented Jun 2, 2021

No, they don't fall through like C or Java. That's a neat idea, but the problem is that people get confused and assume they do fall through. Hence, requiring an explicit break would avoid any confusion. They also allow multiple values in a case, so most of the uses of fall through are still supported:

import uint from std::integer 

function wierd(uint x) -> (uint r)
ensures r > 1:
    //
    switch x:
        case 0,1:
            x = 2
        default:
            assert x >= 2
    //
    return x

Kinda artificial example, but you get the idea. The above would then become:

    switch x:
        case 0,1:
            x = 2
            break
        default:
            assert x >= 2
            break
    //
    return x

Perhaps the last case doesn't need a break though.

Hmmmm, I agree though that the original actually does look better ...

@lojikil
Copy link

lojikil commented Jun 5, 2021

Ja, personally I like the first one better, and it's easier to see how you stack cases: you add N conditions separated by commas, versus allowing a fall through, and since we don't fall through by default anyway, there's no safety change.

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

No branches or pull requests

2 participants