stacks-network / stacks-network/stacks-core

Explicit variant patterns for match expressions

Open
#7,101 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

clarity-naming clarity-next-stretch clarity-non-backwards-compatible clarity-syntax-change
Dominant language
Rust
Stars
3.1k
Forks
762
Avg merge
4d 6h
Merged PRs (30d)
76

Description

The match expression in Clarity would benefit from supporting explicit variants for the match patterns, instead of just symbols to bind for each branch. This will also make the syntax more flexible, ready to adapt to advances like the native enum types proposed in stacks-network/reference#9.

Here is an example of explicit variant patterns to match on response type, with branches for the ok and err variants:

(match res
   (ok value) (+ value 1)
   (err code) code)

This replaces the current syntax:

(match res
   ok-value (+ ok-value 1)
   err-code err-code)

Note that in the current syntax, developers typically will use naming to signal the variant for each of the branches, but it can be obfuscated as it is the order of the branches not the name that matters. In contrast, with explicit variant patterns, the intent for each branch is apparent by default.

See also the proposed if-let construct:
https://github.com/blockstack/stacks-blockchain/issues/1403

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the linked Clarity match documentation and compare the current and proposed pattern examples in this issue. Determine the language and parser entry points that implement match expressions, then define completion as supporting explicit ok and err variant patterns while preserving branch behavior and compatibility with the existing syntax.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.