There is no switch/case statement or method in F-Script and Smalltalk. Usually, switching is done by taking advantage of object polymorphism or by simply nesting ifTrue:IfFalse: messages. For example, here is some C pseudo code with a switch statement:
switch (aValue)
{
case 1: doSomething1;
break;
case 2: doSomething2;
break;
case 3: doSomething3;
break;
}
We can write equivalent code in F-Script using ifTrue:IfFalse: messages:
aValue = 1 ifTrue:[doSomething1] ifFalse:[
aValue = 2 ifTrue:[doSomething2] ifFalse:[
aValue = 3 ifTrue:[doSomething3]]]
Another possible technique is based on blocks and dictionary lookup. Basically, we construct a dictionary whose keys are the various cases’ values we want to check against and whose values are blocks of code to execute. We then do a lookup in the dictionary and execute the selected block. With our example, this gives:
cases := #{
1 -> [doSomething1],
2 -> [doSomething2],
3 -> [doSomething3]
}.
(cases objectForKey:aValue) value
Searching for Smalltalk switch case on the Web will get you to additional techniques and discussions on emulating switch statements.
i think double dispatching should be the way to go, instead of using too many ifTrue:ifFalse:’s or even dictionaries with blocks
My POV has always been (i.e. for about 25 years) that switch syntax in most languages is a workaround for languages that don’t have code blocks or something similar.