PatternMatcher

Description

Pattern Matching syntax for Smalltalk

Details

Source
GitHub
Stars
4
Created
Feb. 19, 2022
Updated
Aug. 6, 2025

README excerpt

# Welcome To PatternMatcher

Because Smalltalk implements conditions and flow control statements as methods, so why not pattern matchers?

# Using

We add an extension method on object...

```smalltalk
100 patternMatchWithRules: {
  PMRule whenMatches: [ :o | o = 42 ] execute: [ 'yes' ].
  PMRule whenMatches: [ :o | o = 1 ]  execute: [ 3  ].
}

"result of ^^^ will be nil, as no patterns match"

42 patternMatchWithRules: {
  PMRule whenMatches: [ :o | o = 42 ] execute: [ 'yes' ].
  PMRule whenMatches: [ :o | o = 1 ]  execute: [ 3  ].
}

"result will be 'yes', as the first expression matches.".
```

An object can only match one rule in a set, and once that happens rules stop evaluating.
← Back to results