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

Allow casing over multiple expressions #222

Open
robinheghan opened this issue Sep 12, 2023 · 0 comments
Open

Allow casing over multiple expressions #222

robinheghan opened this issue Sep 12, 2023 · 0 comments
Labels
accepted The issue has been reviewed and accepted for implementation language proposal proposal of a major feature to the language

Comments

@robinheghan
Copy link
Member

Casing over multiple expressions is something that comes up in several cases. Currently, you can do this by wrapping things up in a record and casing over that.

-- a silly example, I know
case { left = 1, right = 3 } of
  { left = 1 } -> "Left is one!"
  { right = 1 } -> "Right is three!"
  _ -> "Left and right are not ones"

However, this isn't ideal as it requires allocating a data structure in order to perform the comparison, which is bad for hot loops. Also, for simple cases, it adds a bit of boilerplate (the names of the record fields) which, for a local single-used expression, doesn't add a ton of value.

To solve the efficiency problem, without loosing too much readability, one could allow for multiple expressions in a case of expression:

-- a silly example, I know
case 1, 3 of
  1, _ -> "Left is one!"
  _, 1 -> "Right is three!"
  _ -> "Left and right are not ones"

Alternatives

Previously Gren had tuples, which was quite often used for cases like these. However, tuples have the same performance overhead as records (they're compiled to the same thing) and tuples as a datastructre makes code more brittle as everything depends on the order of arguments. Positional semantics aren't that big of a deal in a single expression, but they're a problem when in a data structure.

@robinheghan robinheghan added language proposal proposal of a major feature to the language accepted The issue has been reviewed and accepted for implementation labels Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted The issue has been reviewed and accepted for implementation language proposal proposal of a major feature to the language
Projects
None yet
Development

No branches or pull requests

1 participant