PistonDevelopers / PistonDevelopers/dyon

Pattern matching

Open
#360 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

discussion draft
Dominant language
Rust
Stars
1.9k
Forks
56
Avg merge
1m
Merged PRs (30d)
6

Description

Related to https://github.com/PistonDevelopers/dyon/issues/304

Idea taken from https://github.com/PistonDevelopers/dyon/issues/359, but might be useful in general.

The idea is to add pattern matching to Dyon, a bit similar to Rust. Since Dyon is dynamically typed there is a ?= operator instead of :=.

The ?= operator checks the run-time type at run-time and reports an error if it is wrong. The type checker checks the static types and reports an error if the code never gets called.

If pattern

The ?= operator can be used with if:

x := [1, 2]
if [a, b] ?= x {
     ...
}
Try pattern

It can also be used directly in a function that returns err:

fn foo() -> res {
    x := [1, 2]
    [a, b] ?= x
    ...
}

The following code gives an error because it is exhaustive:

a := 1
b ?= a // ERROR: Use `:=` instead
Match pattern

A match expression similar to Rust:

x := [1, 2]
match x {
    [a, b] => { ... }
    _ => { ... }
}

The match requires _ or a variable since it can not be exhaustively checked.

Type is checked on all arms like with an if-expression.

Contributor guide

No contributing guide indexed for this repository

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

Start by reviewing the related issues 304 and 359, then trace how Dyon parses, type-checks, and evaluates the shown ?= and match constructs. Done means implementing the proposed if, try, and match patterns with the stated runtime and static type errors, including exhaustive cases.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.