Dictu icon indicating copy to clipboard operation
Dictu copied to clipboard

Added fall Keyword for the switch Statement

Open manoharkakumani opened this issue 1 year ago • 7 comments

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
  • [X] New feature
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)

Housekeeping:

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

Screenshots (If Applicable):

image

manoharkakumani avatar Jun 27 '24 07:06 manoharkakumani

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?

Jason2605 avatar Jul 01 '24 14:07 Jason2605

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 
}
}

manoharkakumani avatar Jul 02 '24 07:07 manoharkakumani

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

Jason2605 avatar Jul 02 '24 08:07 Jason2605

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.

manoharkakumani avatar Jul 02 '24 08:07 manoharkakumani

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 avatar Jul 02 '24 09:07 Jason2605

Opinions on this @briandowns?

Jason2605 avatar Jul 07 '24 18:07 Jason2605

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

briandowns avatar Jul 08 '24 01:07 briandowns

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

Jason2605 avatar Nov 06 '24 20:11 Jason2605