-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Forbid constructing tuples via "new" #10891
Comments
I suggest that it should not work. That is, I do not suggest the object creation syntax should support tuple types. Instead we would give an error message that recommends using a tuple literal instead. var x = new (int x1, int x2)(3, 4);
// error: cannot use 'new' with a tuple type. Use '(x1: 3, x2: 4)' instead There are other contexts we might disallow tuple types (e.g. an @MadsTorgersen @dotnet/ldm @jcouv @AlekseyTs What do you think? |
I see no benefit to this syntax either. |
I see no reason to disallow this. |
We let people write: new int?(4) So i somewhat agree with @AlekseyTs, i don't see why this shouldn't be disallowed. That said, i also ascribe to the: everything starts at -100 points. This seems to have pretty darn marginal value. Indeed, given how confusing it looks, i could argue it has anti-value. So i have no problem with it being disallowed. |
One reason to disallow it is that |
Note that there is also a parameterless case - new (int a, int b, int c)();
// same as
default((int a, int b, int c)); It might need to be considered together with parameterized case. |
See #347 (comment)
|
Now Neal is quoting me! |
I have removed "Wont FIx" and made this a bug. I think "new" works currently with tuples <= 7 and not with bigger ones. |
What is the bug exactly? That using tuple type syntax in object creation expression is not allowed? |
I think the bug is that it is allowed (for sufficiently small tuples). |
Semantically |
I don’t think this would be difficult TBH. We’d just need to update “ParseTypeCore” to take a Boolean saying if tuples were allowed. That would just go into ParseUnderlyingType, which would then ignore open parens.
From: VSadov [mailto:notifications@github.com] Semantically new (int, int)(1, 2) is valid since (int, int) type does have a constructor taking two ints. — |
Didn't we decide in a recent LDM meeting that this should be allowed? Bug seems out of date with more recent LDM discussions, wanted to get clarity on whether it was still relevant or not. |
As far as i know it is still supposed to be disallowed. |
Part of the reason we decided to disallow this is that we are considering adding target-typed construction in the future KeyValuePair<int, int> kvp;
kvp = new (1, 2); We need to disallow |
Also, allowing "new" with long tuples is a bit of work, while literals provide all the functionality needed with more concise syntax. |
The following should work (or perhaps not):
new (int x1, int x2, ...., int x12) ( 1,2,...., 12);
It could be more complicated when constructor initializers are involved, like:
new (int x1, int x2, ...., int x12) ( 1,2,...., 12) { x1 = 1, x12 = 2};
If not, then new (int, int)(1, 2) should not work either.
The text was updated successfully, but these errors were encountered: