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

Added fall Keyword for the switch Statement #741

Closed
wants to merge 4 commits into from

Conversation

manoharkakumani
Copy link
Contributor

@manoharkakumani manoharkakumani commented Jun 27, 2024

What's Changed:

Added a new keyword fall to use it in switch statement to execute the next case in the flow

switch (1) {
    case 1: {
        // This block of code is executed!
    }

    fall case 10: {
        //  This block of code is executed!
    }
}

Type of Change:

  • Bug fix
  • New feature
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Housekeeping:

  • Tests have been updated to reflect the changes done within this PR (if applicable).
  • Documentation has been updated to reflect the changes done within this PR (if applicable).

Screenshots (If Applicable):

image

@Jason2605
Copy link
Member

Thanks for the PR, just the one question:

I'm not sure I fully understand the use of the fall keyword here, what is it providing that we can't already do?

@manoharkakumani
Copy link
Contributor Author

manoharkakumani commented Jul 2, 2024

Thanks for the PR, just the one question:

I'm not sure I fully understand the use of the fall keyword here, what is it providing that we can't already do?

fall is a keyword used within a switch statement to specify that execution should continue to the next case, even if the matching case block completes.

In our language our switch statement is implemented to executed only first matched case block #410. In some sequential scenario you might want to execute code in one case and then continue with the next case, regardless of whether its condition matches.

for example, you are managing subscriptions [ all the premium members should have premium features and basic features whereas basic members should have only basic features]

// before fall keyword

var membership = "premium";
switch(membership) {
case "premium" : {

// premium features + basic features 
}
case "basic " : {
// basic features 
}
}
// with fall keyword

var membership = "premium";
switch(membership) {
case "premium" : {

// premium features 
}
fall case "basic " : {
// basic features 
}
}

@Jason2605
Copy link
Member

Thanks for the PR, just the one question:
I'm not sure I fully understand the use of the fall keyword here, what is it providing that we can't already do?

fall is a keyword used within a switch statement to specify that execution should continue to the next case, even if the matching case block completes.

In our language our switch statement is implemented to executed only first matched case block #410. In some sequential scenario you might want to execute code in one case and then continue with the next case, regardless of whether its condition matches.

for example, you are managing subscriptions [ all the premium members should have premium features and basic features whereas basic members should have only basic features]

// before fall keyword

var membership = "premium";
switch(membership) {
case "premium" : {

// premium features + basic features 
}
case "basic " : {
// basic features 
}
}
// with fall keyword

var membership = "premium";
switch(membership) {
case "premium" : {

// premium features 
}
fall case "basic " : {
// basic features 
}
}

I think my confusion comes from the fact that if you always wanted "basic" to run you'd just move it out of the switch:

var membership = "premium";
switch(membership) {
    case "premium" : {
        // premium features 
    }
}

// basic features 

I'm trying to think of a case currently where you'd need the fall keyword without a ton of additional boilerplate

@manoharkakumani
Copy link
Contributor Author

Thanks for the PR, just the one question:
I'm not sure I fully understand the use of the fall keyword here, what is it providing that we can't already do?

fall is a keyword used within a switch statement to specify that execution should continue to the next case, even if the matching case block completes.
In our language our switch statement is implemented to executed only first matched case block #410. In some sequential scenario you might want to execute code in one case and then continue with the next case, regardless of whether its condition matches.
for example, you are managing subscriptions [ all the premium members should have premium features and basic features whereas basic members should have only basic features]

// before fall keyword

var membership = "premium";
switch(membership) {
case "premium" : {

// premium features + basic features 
}
case "basic " : {
// basic features 
}
}
// with fall keyword

var membership = "premium";
switch(membership) {
case "premium" : {

// premium features 
}
fall case "basic " : {
// basic features 
}
}

I think my confusion comes from the fact that if you always wanted "basic" to run you'd just move it out of the switch:

var membership = "premium";
switch(membership) {
    case "premium" : {
        // premium features 
    }
}

// basic features 

I'm trying to think of a case currently where you'd need the fall keyword without a ton of additional boilerplate

let's consider one more case called "premium+" [ which has some more additional features to premium]. How do you handle it.

@Jason2605
Copy link
Member

Ah so you're saying in that circumstance you could then do something like this?

switch (membership) {
    case "premium+": {
        // premium+
    }

    fall case "premium": {
        // premium
    }

    fall case "basic": {
        // basic
    }
}

Where premium+ would fall into both cases and premium would fall into basic?

@Jason2605
Copy link
Member

Opinions on this @briandowns?

@briandowns
Copy link
Contributor

I get the idea behind the addition to the language but I'm not really sure this feature is necessary. I think there are probably better ways to handle those cases. As always, I'll defer to your judgement. :D @Jason2605

@Jason2605
Copy link
Member

Gonna close this out for now, thanks for the PR though @manoharkakumani

@Jason2605 Jason2605 closed this Nov 6, 2024
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

Successfully merging this pull request may close these issues.

3 participants