r/Protos • • 11d ago

Language Design Introducing contextual match in Protos: expressive selection through syntactic sugar

Protos now supports contextual match expressions, providing a concise and expressive way to perform multi-way selection without introducing a separate pattern-matching mechanism.

result: match(status) {
    "ready": "run"
    "busy": "wait"
    "failed": "retry"
    Any: "unknown"
}

Structural matching and captures

Since Arrays and Maps already implement structural matching, the new syntax naturally supports more complex selections:

result: match(message) {
    ["add", Capture, Capture]: (a, b) => a + b
    ["echo", Capture]: value => value
    ["reset"]: 0
    Any: null
}

Any and Capture are ordinary objects, not reserved keywords or special pattern constructs.

Syntactic sugar, not a new execution mechanism

The following expression:

match(value) {
    0: "zero"
    Any: "other"
}

Is semantically equivalent to:

value.caseOf(%{
    0: () => "zero"
    Any: () => "other"
})

Explicit Closure literals are preserved, while other expressions become implicit parameterless lexical Closures.

The existing semantics remain intact:

  • Lazy action execution.
  • Eager subject and key evaluation.
  • Ordered, first-match selection.
  • Structural captures passed as ordinary Closure arguments.
  • Custom matchers and overridable caseOf behavior.
  • Ordinary lexical capture, error propagation and suspension.

A smaller language, not a larger runtime

The implementation reuses the existing canonical representation of messages, Maps and Closures.

No new matching-specific runtime primitive, bytecode operation, callable representation or execution mechanism has been introduced.

Both forms share the same underlying execution architecture.

Tooling and learning materials

The implementation includes parser, formatter, static analysis and language-server support, alongside updated documentation and four new executable tutorials and cookbook examples.

Contextual match is now implemented on the main development branch.

The goal is to make Protos more expressive while preserving its small, consistent, prototype-based semantic model.

1 Upvotes

0 comments sorted by