effekt-lang / effekt-lang/effekt
List/collection patterns
- Dominant language
- Scala
- Stars
- 469
- Forks
- 41
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 23
Description
## Motivation
Effekt has list literals, but no list patterns, therefore one cannot use lists in patterns :(
To remedy this, we could allow, let's say:
```scala
[1, 2, 3] match {
case [1] =>
case [1, 2] =>
case [] =>
case [1, ..rest] =>
case [1, 2, ..rest] =>
case [head, ..rest] =>
case _ =>
}
```
as "syntax sugar" for:
```scala
[1, 2, 3] match {
case Cons(1, Nil()) =>
case Cons(1, Cons(2, Nil())) =>
case Nil() =>
case Cons(1, rest) =>
case Cons(1, Cons(2, rest)) =>
case Cons(head, rest) =>
case _ =>
}
```
### Reasoning
I thought about this syntax specifically in order to also _later_ allow:
1. merging two lists/collections as `[..xs, ..ys]`
2. record updates #557:
```scala
record Foo(a: Int, b: Int)
val default = Foo(12, 9) // or: Foo(a = 12, b = 9)
val x = Foo(a = 10, ..default)
println(x) // Foo(10, 9)
```
### Questions
Feel free to bikeshed:
1. `first :: rest` vs `[first, ..rest]`: I like the latter because I feel like it's easier to scale it for patterns like `[1, 2, 3, ..rest]`, on the other hand `::` is definitely more friendly for LLMs coming from Scala
2. the exact syntax for `..` (`...`, `*`): here I took inspiration from C# / Rust / JS / Swift(ish) / Zig(ish)
3. should it be `..rest` or `rest @ ..` ala Rust (making the `..` an irrefutable pattern)? or something else like `rest..`? the `@` variant would force us to also newly add `@` patterns!
4. should we also allow `[..init, 1]` to match against the last element? It would be consistent, but might lead to programs with unusually bad perf...
5. how would we take advantage of this / something similar _later_ for other collections? what about streams?
6. do we need/want this at all?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.