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

[BUG] Assignment of multiple return values #543

Open
MaxSagebaum opened this issue Jul 14, 2023 · 4 comments
Open

[BUG] Assignment of multiple return values #543

MaxSagebaum opened this issue Jul 14, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@MaxSagebaum
Copy link
Contributor

Describe the bug
Cpp2 does not seem to be able to capture multiple return values.

To Reproduce

  1. Sample code: https://cpp2.godbolt.org/z/eW66nP4eq
f: () -> (a: int, b: int) = {
    a = 1;
    b = 2;
}

main: (args) -> int = {
    i: int = 0;
    j: int = 0;

    // (i, j) = f();  // Option1: Parse error
    // i ,j = f();    // Option2: Parse error
    // {i, j} = f();  // Option3: Parse error
    // (s: int, t: int) = f(); // Option4: Parse error

    i = f();          // Obiously: Cpp compiler error

    return 0;
}
  1. Expected result:
    I would expect that either option 1,2 or 3 works.

  2. Actual result/error
    Option 1,2,3,4:

example.cpp2(6,23): error: ill-formed initializer (at '{')
example.cpp2(6,1): error: unexpected text at end of Cpp2 code section (at 'main')
example.cpp2(1,0): error: parse failed for section starting here

Additional context
I found this because I wanted to write a counterexample for #540

@MaxSagebaum MaxSagebaum added the bug Something isn't working label Jul 14, 2023
@JohelEGP
Copy link
Contributor

See also
#500,
#325, and
#449 (comment).

@hsutter
Copy link
Owner

hsutter commented Jul 14, 2023

Quick ack: Right, this isn't currently allowed but I intend it to be allowed.

My current thought (which I almost implemented last month) was to allow this (possibly with parens)

a, b, c = f();

with the semantics that it would lower to this

std::tie(a, b, c) = f();

However, I'm thinking about the suggestions in #540 which could lead to a different meaning, so I'm glad I didn't implement it yet, to have time to think about this more.

Similarly, there isn't yet a destructuring declaration syntax in Cpp2, and there will need to be (it's also on the list) and it should be consistent with the destructuring initialization/assignment syntax.

@MaxSagebaum
Copy link
Contributor Author

See also
#500,
#325, and
#449 (comment).

Ok, thanks. I did a quick check but did not find anything with return values.

What about option 4. Should this also be valid? With the approach of cpp2 to delay construction until the first assignment. It is actually not problem. If it is not allowed we would see something like this:

i: int;
j: int;
i, j = f();

which is, in my opinion, quite ugly. Without the braces it would actually look quite natural:

i: int, j: int = f();

Braces would help here to understand, that not only j is initialized.

@JohelEGP
Copy link
Contributor

See also
#500,
#325, and
#449 (comment).

Ok, thanks. I did a quick check but did not find anything with return values.

They want similar syntax for structured bindings,
so they should be considered together.
The last one mentions:

// For assignment:
(a, b) = foo();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants