Switch

Description

Smalltalk Switch Case implementation

Details

Source
GitHub
License
GPL-3.0
Created
Feb. 28, 2025
Updated
Dec. 18, 2025
Topics
smalltalk switch switch-case vasmalltalk vastplatform

README excerpt

# Switch
Nice and Tiny implementation of the Switch-Case mechanism used in procedural programming languages.
Although it does not 100% match to the standard it will fulfill most of the commonly used cases.
# Properties
* Fall through based
* Imitate label based approach used in C
* Break command to interrupt fall through
# Differences
* default has to be the last operation if used
* Selector matches a wide variety of tags if they have implemented the equal method "="
* Additionally you can use: Collection / Interval and Block as a tag
# Examples
## Standard
```
Switch ? selector
  case: 1 exec: [ "Execute for 1" ];
  case: 2 exec: [ "Execute for 1 or 2" ];
  break;
  case: 3 exec: [ "Execute for 3" ];
  default: [ "No match" ].
```
## Collection / Block
```
Switch ? selector
  case: ( 1 to: 2 ) execAndBreak: [ "Execute for 1 or 2" ];
  case: [ 9 sqrt asInteger ] exec: [ "Execute for 3" ];
  case: #( 4 5 ) exec: [ "Execute for 3 or 4 or 5" ];
  default: [ "No match" ].
```
## Advanced symbolic syntax
Whoever is a fan of using pictograms instead of keyword can substitute the keyword #exec: or #execAndBreak: by their symbolic replacement.
```
Switch ? selector
  case: 1 => [ "Execute for 1" ];
  case: 2 =| [ "Execute for 1 or 2" ];
  case: [ 1+2 ] =| [ "Execute for 3" ];
  case: #( 4 5 ) => [ "Execute for 4 or 5" ];
  default: [ "No match" ].
```
* exec: can be substituted by: **=>**
* execAndBreak: can be substituted by: **=|**
← Back to results