dictu-lang / Dictu

Dictu is a high-level dynamically typed, multi-paradigm, interpreted programming language.
https://dictu-lang.com
MIT License
268 stars 53 forks source link

Added fall Keyword for the switch Statement #741

Open manoharkakumani opened 2 months ago

manoharkakumani commented 2 months ago

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:

#

Housekeeping:

#

Screenshots (If Applicable):

image

Jason2605 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

Opinions on this @briandowns?

briandowns commented 2 months ago

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