ark-lang / ark

A compiled systems programming language written in Go using the LLVM framework
https://ark-lang.github.io/
MIT License
676 stars 47 forks source link

Pattern Matching #643

Open MovingtoMars opened 8 years ago

MovingtoMars commented 8 years ago

Value matching

match 5 {
    5 -> io::println("yes");
}

Range Matching

match 5 {
    1..10 -> io::println("yes");
}

Multiple Pattern Matching

match 5 {
    1 | 2..4 | 5 | 7 -> io::println("yes");
}

Matching with a conditional

match 5 {
    5 if 1 == 2 -> io::println("never executed");
    5 if 1 == 1 -> io::println("yes");
}

Destructuring

enum Enum {
    Simple,
    Tuple(s8, u32),
    Struct{a: int, b: int},
    ...
}

Enum::Tuple(_, that) := someInstance; // destructuring in assignment

match someInstance {
    Simple -> ...
    Tuple(x, _) -> ... // destructuring tuple in match
    Struct{a: x} -> ... // destructuring struct in match
    _ -> ... // default case
}
felixangell commented 8 years ago

Boom found it, though we still haven't decided on the patterns for inclusive and exclusive ranges... which is really something we need to figure out since we've been debating it ever since. @kiljacken @vnev @raoulvdberge @MovingtoMars

kiljacken commented 8 years ago

As long as we do support for custom iterators, Liam's proposal of n .. m including n excluding m, is probably fine to go with.

On Fri, Jan 22, 2016, 13:55 Felix Angell notifications@github.com wrote:

Boom found it https://github.com/ark-lang/ark/issues/534, though we still haven't decided on the patterns for inclusive and exclusive ranges... which is really something we need to figure out since we've been debating it ever since. @kiljacken https://github.com/kiljacken @vnev https://github.com/vnev @raoulvdberge https://github.com/raoulvdberge @MovingtoMars https://github.com/MovingtoMars

— Reply to this email directly or view it on GitHub https://github.com/ark-lang/ark/issues/643#issuecomment-173913368.

MovingtoMars commented 8 years ago

Just updated.

felixangell commented 8 years ago

I should add that the match statement is exhaustive, i.e. it wants every possible outcome specified, or a default case to handle unspecified conditions.

kiljacken commented 8 years ago

The enum matching and destructuring part is implemented as of #686. The rest, including exhaustiveness checking, still remains.

SamTebbs33 commented 8 years ago

How about using commas for matching multiple values?

match 5 {
    1, 2..4, 5, 7 -> io::println("yes");
}

This then makes | available when matching against boolean expressions.

kiljacken commented 8 years ago

:+1:

felixangell commented 8 years ago

:arrow_up: @SamTebbs33