Skip to content

Conversation

@richcarl
Copy link
Contributor

@richcarl richcarl commented Dec 7, 2024

Implements EEP 77 (erlang/eep#74)

It would be useful to be able to easily bind variables in the qualifiers of a comprehension, for example:

[Z || X <- Ls,
      {foo, Y} = g(X),
      Z <- f(Y, h(Y))].

You can do this today by writing a singleton generator:

[Z || X <- Ls,
      {foo, Y} <- [g(X)],  % produce single element
      Z <- f(Y, h(Y))].

but this has some drawbacks: the intent is not clear to the reader; you have to remember to write a single element list around the right hand side argument (which itself could be a list); you probably want it to be a strict generator so typos don't just silently yield no elements; and someone might edit the code later and accidentally add extra elements to the right hand side, causing unintended Cartesian combinations.

It is in fact already allowed syntactically to have a = match expression in the qualifiers, but this is just interpreted as any other expression - thus expected to produce a boolean value, and if false, the current element will be skipped. Hence, any qualifier to the right of a V = Expr match will only execute if V is true. We can therefore expect that no such uses exist in practice. (The OTP code base has been checked and does not contain any.) To be completely safe, matches in a comprehension qualifier position can first be made an error, and then allowed with these binding semantics in the following major release.

This is related but orthogonal to #9134. It would make those few cases where variables have been exported from subexpressions of qualifiers much nicer to rewrite than requiring them to be singleton generators.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 7, 2024

CT Test Results

     6 files     571 suites   1h 59m 16s ⏱️
 4 449 tests  4 320 ✅ 129 💤 0 ❌
10 629 runs  10 473 ✅ 156 💤 0 ❌

Results for commit 1c1c525.

♻️ This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

@richcarl
Copy link
Contributor Author

richcarl commented Dec 7, 2024

Correction: 1 occurence in the OTP test suites, in singleton_inference.erl, which is weird enough that I won't even try to rewrite it myself. Maybe @jhogberg who wrote it (commit ebc461e) can help?

@rickard-green rickard-green added the team:LG Assigned to OTP language group label Dec 9, 2024
@richcarl
Copy link
Contributor Author

Turned into a feature. Becomes an error if it occurs when the feature is not enabled.

Also fixed multiple problems with the features implementation.

@richcarl richcarl force-pushed the lc-match-operator branch 3 times, most recently from 5f98841 to 0ddf501 Compare February 3, 2025 15:51
@richcarl richcarl force-pushed the lc-match-operator branch 2 times, most recently from 24cd657 to 30293d7 Compare November 15, 2025 21:19
@richcarl
Copy link
Contributor Author

Rebased on #10376 to make the Features fix a separate thing.

@richcarl
Copy link
Contributor Author

Rebased again and updated feature introduced version to 29.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team:LG Assigned to OTP language group

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants