Case Select expression conditions

Monkey Forums/Monkey Programming/Case Select expression conditions

Nobuyuki(Posted 2011) [#1]
Coming from a VB.NET background, I'm kinda used to be able to use the "is" token to qualify an expression with an explicit comparison operator, such as the following:

Dim a As Int = Rnd*10
Select Case a
Case 0    'This is the normal syntax in Monkey, as well
    Print "Zero.  What are the odds of that?"
Case is = 1 
'Explicit equality comparison.  Same effect as above, different syntax
    Print "One is the loneliest number"
Case is > 9
'Explicit greater-than comparison.
    Print "My, what a large number."
Case Else  'Special case syntax
    Print "An average number."
End Select


I'm pretty sure in monkey this isn't possible, which unfortunately takes away a lot of potential usage for a Case Select when it comes to enumerations or variables with a significant number of potential cases to evaluate explicitly for equality. My question I guess is, is VB's syntax just some syntactic sugar, or are there technical issues to implementing a different comparison operator in a Case Select?

Of course, everyone could just use a bunch of verbose If statements, but where's the fun in that.....


Jesse(Posted 2011) [#2]
you can do it something like this:

Select true
   Case a = 0

   Case a = 1

   Case a > 9

   Default

End Select



Shinkiro1(Posted 2011) [#3]
If you wonder: Default = Case Else


Raz(Posted 2011) [#4]
Jesse: That's genius, I've never considered something like that before :D


Nobuyuki(Posted 2011) [#5]
Jesse: Thanks ! That's so crazy and yet brilliant, why didn't I think of that xwx

(Wonder if this works in Strict, too....)


Samah(Posted 2011) [#6]
What's wrong with:
If a = 0
  ' stuff
ElseIf a = 1
  ' stuff
ElseIf a > 9
  ' stuff
Else
  ' stuff
End



wiebow(Posted 2011) [#7]
Nothing. It's just different.


Nobuyuki(Posted 2011) [#8]
Samah: What's wrong with...


The syntax is pretty similar to Case Select when using Jesse's example, however, the VB.NET syntax is pretty useful when testing for an expression to be evaluated that's particularly long. This is especially true if you're the kind of person who TendsToWriteVariables.LikeThis ;)


Samah(Posted 2011) [#9]
@wiebo: Nothing. It's just different.

Actually it's more than just different. In Java, I usually only use a switch for enums because the compiler can catch when you're missing a case. The only other time is if I want to use a fallthrough (which Monkey doesn't do). It would be nice if Monkey had official support for enums.
I'll admit that I used to use the Select True trick in VB6 to check for selected radio buttons.

@Nobuyuki: This is especially true if you're the kind of person who TendsToWriteVariables.LikeThis ;)

Nothing wrong with that... :D


Sensei(Posted 2014) [#10]
Is it just me or can I not do this in Monkey?

select y
case 1
	x = 10
case 2
case 3
case 4
	x = 20
case 10
	x = 30
end


In the above example, in Javascript, x will be set to 20 in cases 2, 3 and 4. However, in Monkey, it seems to only trigger in case 4. At least that's the result I seem to be getting, unless I'm doing something wrong?

Anybody know? I will change the code to if statements instead if I have to, but I find using select statements to be cleaner.


therevills(Posted 2014) [#11]
Try this Jaco:

Strict

Function Main:Int()
	Local y:Int = 3
	Local x:Int = 0
	
	Select y
		Case 1
			x = 10
		Case 2, 3, 4
			x = 20
		Case 10
			x = 30
		Default
			x = -1
	End
	Print x

	Return True
End



Sensei(Posted 2014) [#12]
Many thanks matey.

Ugh, I've just now seen it in the documentation. If only I scrolled down a bit more :)


Gerry Quinn(Posted 2014) [#13]
'Select True' is a very ingenious solution.

The only worry is that it's probably an accidental Monkey feature resulting from the implementation of Select..Case as a sequence of Ifs. If that ever changed it might break...

Of course the code would be easy to fix in such cases by changing to an explicit series of Ifs.


Midimaster(Posted 2014) [#14]
I also think the features of SELECT/CASE in monkey are very limited. The workarounds you all offer are not really usable solutions for complex variable names....

I would like to see:

Select LongWordClassName.LongFunctionName(Parameter)
     Case 3
          ....blabla
     Case 4 To 11
          ....blabla
     Case >11
          ....blabla
End





Gerry Quinn(Posted 2014) [#15]
Well, there is the idiom:

Local param:Int = LongWordClassName.LongFunctionName(Parameter)
Select param...


Paul - Taiphoz(Posted 2016) [#16]
old thread I know but I'm sitting looking at a Select / case with 150+ statements, and was wondering if this could be done.

Select Thing
    Case #
         Print "the compiler or interpreter or IDE? would replace each Case # with Case 1,2,3 etc incrementally"
    Case #
         Print "this would be the second thing called. 
End Select


If I want to slide something into the middle of my really large case block I need to renumber everything from the insert point down to the bottom of the list which is annoying.


Nobuyuki(Posted 2016) [#17]
It would be cool to see the VB style syntax in mx2, especially considering now that operator overloads are possible.