nenu-lang / match
Stable

Match

  • match evaluates one subject and compares it against ordered to arms.
  • One arm may group several non-destructuring patterns or destructure one structured variant—but not both.
  • Use field: alias while destructuring when the local name should differ.

Basic Match Syntax

code
1match expense.status {2  to ExpenseStatus.Draft {3    label = "Draft"4  }5  to ExpenseStatus.Submitted {6    label = "Waiting for approval"7  }8  else {9    label = "Reviewed"10  }11}

The subject after match is evaluated once. Arms are checked in order. The first matching arm runs. else handles values not covered above.

Literal Patterns

code
1match status_code {2  to 200 {3    message = "Success"4  }5  to 404 {6    message = "Not found"7  }8  to 500, 502, 503 {9    message = "Server error"10  }11  else {12    message = "Unknown status"13  }14}

A pattern may be a number or string literal as well as an enum variant. Group literal patterns with commas when they share one body. Pattern values must be compatible with the matched subject.

Grouping Unit Patterns

code
1match expense.status {2  to ExpenseStatus.Draft, ExpenseStatus.Submitted {3    editable = true4  }5  else {6    editable = false7  }8}

Separate several non-destructuring patterns with commas when they perform the same work. The arm still has one body.

Destructuring A Structured Variant

code
1match expense.status {2  to ExpenseStatus.Approved(approved_at) {3    reviewed_at = approved_at4  }5  to ExpenseStatus.Rejected(reason: rejection_reason) {6    message = rejection_reason7  }8  else {9    message = "Not reviewed"10  }11}
  • A bare field binds to a local with the same name.
  • field: alias binds the field to a different local name.
  • The bindings exist only inside that arm.
  • One destructuring arm handles one structured pattern.

Grouping And Destructuring Do Not Mix

code
1to ExpenseStatus.Draft, ExpenseStatus.Submitted {2  editable = true3}

Match Or If

SituationPrefer
Several branches inspect the same enum or tagged valuematch
One or two unrelated boolean conditionsif
Need structured variant fieldsmatch destructuring
Need numeric ranges or combined comparisonsif

Common Mistakes

WrongCorrect
switch statusmatch status
case ExpenseStatus.Draftto ExpenseStatus.Draft
Destructure several patterns in one armUse one arm per destructured variant.
Use an arm binding after the armCopy the needed result into an outer local.

Continue Learning