Skip to content

Match

Name Mandatory Description Default Type
⬅️ Input The value that's compared with the declared cases. Any
Output ➡️ Same value as input if :Passthrough is true else the output of the matched case's shard if :Passthrough is false. Any
Cases No Values to match against the input. A none case will match anything. [] [Any]
Passthrough No Parameter to control the shard's output. true allows the Match shard's input itself to appear as its output; false allows the matched shard's output to appear as Match shard's output. true Bool

Compares the input with the declared cases EXACT values (use Cond for more complex matching logic) in order of the declaration and activates the shard of the first matched case.

Details

Match compares its input with every case declared via the Cases parameter (in the order of their declaration) till a match is found.

Once a match is found the shard of that matched case is activated/executed and Match execution stops. All subsequent cases (even matching ones) are ignored.

A none case matches anything, so it's a good practice to declare a none case at the end of Cases to execute some default logic if no valid matches exist for a given input. If you do not have a none case, then a non-matching input to Match will fail the shard.

A note on Passthrough

The Passthrough parameter can control the final output of the shard it applies to.

Setting this parameter to true allows the original input of a shard to pass through as its output as well. If this parameter is set to false, passthrough is suppressed and then the output of the shard is the actual computed value coming out from the shard execution. When set to false, the output of all cases needs to be of the same type.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
; single match + nil match at end + no passthrough
1 | Match([
  2 {"Matched 2"} ; case processed, match not found
  1 {"Matched 1"} ; case processed, match found
  3 {"Matched 3"} ; case ignored
  none {"Matched nil"} ; case ignored
] Passthrough: false) | Assert.Is("Matched 1" Break: true)

; multiple matches + nil match at end + no passthrough
1 | Match([
  1 {"Matched 1a"} ; case processed, match found
  1 {"Matched 1"} ; case ignored
  2 {"Matched 2"} ; case ignored
  none {"Matched nil"} ; case ignored
] Passthrough: false) | Assert.Is("Matched 1a" Break: true)

; multiple matches + nil match at start + with passthrough
1 | Match([
  none {Msg("Matched nil")} ; case processed, match found
  1 {Msg("Matched 1")} ; case ignored
  1 {Msg("Matched 1a")} ; case ignored
  2 {Msg("Matched 2")} ; case ignored
] Passthrough: true) | Assert.Is(1 Break: true)

; no matches + nil match in the middle + with passthrough
1 | Match([
  2 {Msg("Matched 2")} ; case processed, match not found
  none {Msg("Matched nil")} ; case processed, match found
  3 {Msg("Matched 3")} ; case ignored
] Passthrough: true) | Assert.Is(1 Break: true)

[info] [sample-wire] Matched nil
[info] [sample-wire] Matched nil