Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List/collection patterns #821

Open
jiribenes opened this issue Feb 7, 2025 · 0 comments
Open

List/collection patterns #821

jiribenes opened this issue Feb 7, 2025 · 0 comments
Labels
feature New feature or request requires-design

Comments

@jiribenes
Copy link
Contributor

jiribenes commented Feb 7, 2025

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:

[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:

[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 Record update syntax? #557:
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?
@jiribenes jiribenes added feature New feature or request requires-design labels Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request requires-design
Projects
None yet
Development

No branches or pull requests

1 participant