A useful control structure in many programming languages is a multiway switch statement, which permits the selection of one out of several possibilities based on the value of some selection key. Using cascades and blocks we can implement a multiway switch in Smalltalk. The class Switch will use the message new: both as a creation message and to assign the switch selection key. The message case:do: will compare the first argument to the selection key, and, if equal, the second argument (a block) will be evaluated. The message test:do: uses blocks for both arguments. The first argument, a one parameter block, is evaluated using the selection key; if it returns true, the second argument is evaluated. A flag is maintained by each instance of Switch indicating whether any condition has been satisfied. The message default: executes the argument, a block, if no previous condition has been met.
For example, the following statement uses a variable suit representing the suit of a card from a deck of cards. It places into color a string representing the color of the card.
Switch new: suit :
case: #spade do: [ color $\left.\leftarrow^{\prime} b_{l a c k}{ }^{\prime}\right]$;
case: #club do: [ color $\leftarrow$ 'black' ] ;
test: $\left[: x \mid\left(x=\right.\right.$ #diamond) or: $[x=$ #heart $]$ do: [ color $\leftarrow^{\prime}$ red' $]$;
default: [ self error: 'unknown suit ', suit ]
Provide a class description for Switch.