Erlang provides pattern matching in its very syntax, which the receive statement uses to its advantage. Since Python does not provide pattern matching in its syntax, we must use a slightly different mechanism to match messages. The first argument passed to the Receiver.addHandler() method can be any Python value. The addHandler() method uses this value as a pattern and interprets it in the following way:
Pattern | Matches | Non-Matches |
---|---|---|
Any | 'text', 13.7, (1, '', lambda: true) | |
'land shark' | 'land shark' | 'dolphin', 42, [] |
13.7 | 13.7 | 'text', 13.6, {'A': 14} |
int | 13, 42, 0 | 'text', 13.7, [] |
str | 'plumber', '' | 42, 0.9, lambda: True |
lambda x: x > 20 | 42, 100, 67.7 | 13, 0, -67.7 |
(str, int) | ('shark', 42), ('dolphin', 0) | ['shark', 42], ('dolphin', 42, 0) |
(str, 20, lambda x: x < 0) | ('shark', 20, -54.76), ('dolphin', 20, -1) | ('shark', 21, -6), (20, 20, -1), ('', 20) |
['A', str, str] | ['A', 'B', 'C', 'D'], ['A', 'B'] | ['C', 'B', 'A'], ['A'] |
[str, int] | ['dolphin', 42, 0], ['shark'] | [42, 0], ['dolphin', 42, 'shark'] |
[Any] | ['dolphin', 42, 0.9], [] | ('dolphin', 42, 0.9), 'shark' |
{'S': int, 19: str} | {'S': 3, 19: 'foo'}, {'S': -65, 19: 'bar', 'T': 'me'} | {'S': 'Charlie', 19: 'foo'}, {'S': 3} |