B4J Question Comma after "Case" in Select-Case leads to catch all

Chris2

Active Member
Licensed User
Longtime User
I stumbled across this by accident. My question is, is this deliberate or a bug?
B4X:
    Dim i As Int = 4564564

    Select i
        
        Case, 1, 2      <----- note the comma directly after Case
            
            Log("1 or 2")
            
    End Select
Prints "1 or 2".
There's no syntax error or other warning in the IDE.
 
Solution
bug is a word that you use at your own risk around here.

a "caseless" case will not compile in a switch block in java.
so the ability to handle a "caseless" case in b4x was a deliberate
decision. one could argue that it might better be handled differently.

run the routine as normal and look at the resultant java code.
then remove the "," after "case" and run again.
look at the resultant java code.
you'll see that an empty case does 2 things: it generates a default action
(aka, "case else"), the other cases are simply ignored and construction of
the switch block in java aborts.

when you remove the empty case, the resultant java code behaves as
expected.

as for me, i would prefer a warning (at least) for the empty
case. i would...

drgottjr

Expert
Licensed User
Longtime User
bug is a word that you use at your own risk around here.

a "caseless" case will not compile in a switch block in java.
so the ability to handle a "caseless" case in b4x was a deliberate
decision. one could argue that it might better be handled differently.

run the routine as normal and look at the resultant java code.
then remove the "," after "case" and run again.
look at the resultant java code.
you'll see that an empty case does 2 things: it generates a default action
(aka, "case else"), the other cases are simply ignored and construction of
the switch block in java aborts.

when you remove the empty case, the resultant java code behaves as
expected.

as for me, i would prefer a warning (at least) for the empty
case. i would also like to see a warning for a missing "case else".
(without it, the programmer and the user see nothing if none of the
available cases is matched. not particularly helpful.)

a "caseless" case makes no sense. (note: an empty string - "" - is not a
caseless case.) exceptions are often generated for errant or missing punctuation.
turning a caseless case into the default behavior may not be what anybody wants.
and case else serves an important purpose. its absence should prompt some comment
from the compiler.
 
Upvote 0
Solution

Chris2

Active Member
Licensed User
Longtime User
bug is a word that you use at your own risk around here.
I meant no offence to anyone. Perhaps a better question may have been simply 'what's going on here?'.

Thanks for your explanation. I understand what's happening now and why.
 
Upvote 0

Chris2

Active Member
Licensed User
Longtime User
Just as a follow up:
Would I be correct to say that it is good practice for all Select-Case blocks to cover all possible cases? Even if that means just including somthing like:
B4X:
Case Else
        Log("Do nothing")


i would prefer a warning (at least) for the empty case
I agree that would be handy. It would make it easier to find a problem caused by this particular typo. I'll add it to the 'wish' forum along with a suggestion to add this behaviour to the documentation.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
as i said, use of case else always is best practice. "do nothing" serves no real purpose except to point out that the given case was not matched. you would always need to capture and log the else case in order to inform the user and the developer. the given case might actually be something - although technically legal - that you do not want to allow. it might be something you as developer never thought of and want to handle. it also might simply be a typo which the user could correct and resubmit after being informed of the situation.

i see you have made your wish public. have a good day.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…