-
-
Notifications
You must be signed in to change notification settings - Fork 686
End the deprecation period of using the result of a comma expression. #7813
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| The deprecation period of using the result of comma expression has ended | ||
|
|
||
| Comma expressions have proven to be a frequent source of confusion, and bugs. | ||
| Using their result will now trigger an error message. | ||
|
|
||
| $(P The comma operator (`,`) allows executing multiple expressions and | ||
| discards the result of them except for the last which is returned. | ||
|
|
||
| --- | ||
| int a = 1; | ||
| int b = 2; | ||
| bool ret = a == 2, b == 2; // true | ||
| --- | ||
|
|
||
| It's also common to use the comma operator in for-loop increment | ||
| statements to allow multiple expressions. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be clearer that this use case is not affected. |
||
|
|
||
| --- | ||
| for (; !a.empty && !b.empty; a.popFront, b.popFront) | ||
| --- | ||
|
|
||
| Hence, using the comma operator in for-loop increment statements is still allowed. | ||
| ) | ||
| $(H4 Corrective Action) | ||
| $(P If possible, split the comma operator in two statements. Otherwise use | ||
| lambdas. | ||
|
|
||
| --- | ||
| auto result = foo(), bar(); | ||
|
|
||
| // split off in two statements | ||
| foo(); | ||
| auto result = bar(); | ||
|
|
||
| // or use lambdas | ||
| auto result = {foo(); return bar();}(); | ||
| --- | ||
| ) | ||
| $(H4 Rationale) | ||
| $(P The comma operator leads to unintended behavior (see below for a selection) | ||
| Moreover it is not commonly used and it blocks the ability to implement tuples | ||
| as a language features using commas. | ||
|
|
||
| A selection of problems through the accidental use of the comma operator: | ||
| --- | ||
| writeln( 6, mixin("7,8"), 9 ); // 6, 8, 9 | ||
|
|
||
| template vec(T...)(T args) { ... } | ||
| vec v = (0, 0, 3); // 3, because vec is variadic | ||
|
|
||
| int a = 0; | ||
| int b = 2; | ||
| if (a == 1, b == 2) { | ||
| // will always be reached | ||
| } | ||
|
|
||
| void foo(int x, int y=0) {} | ||
| foo((a, b)); // Oops, foo is called with x=b | ||
|
|
||
| synchronized (lockA, lockB) {} | ||
| // isn't currently implemented, but still compiles due to the comma operator | ||
| --- | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -136,7 +136,8 @@ static assert(bazra(14)==64); | |
|
|
||
| void moreCommaTests() | ||
| { | ||
| auto k = (containsAsm(), containsAsm()); | ||
| (containsAsm(), containsAsm()); | ||
| auto k = containsAsm(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh? Shouldn't this be: instead? This change doesn't make sense to me.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought it's nice to leave the use of the comma expression in. Here we can do this as
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see why now. Nevermind, carry on. :-P |
||
| for (int i=0; i< k^^2; i+=StructWithCtor(1).n) {} | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -324,23 +324,25 @@ void test9525() | |
| enum result1 = "S6mangle8test9525FZ"~tl!"26"~"__T5test1S"~tl!"13"~id!("6mangle","QBc")~"5f9525Z"~id!("5test1","Qr")~"MFZ1S"; | ||
| enum result2 = "S6mangle8test9525FZ"~tl!"26"~"__T5test2S"~tl!"13"~id!("6mangle","QBc")~"5f9525Z"~id!("5test2","Qr")~"MFNaNbZ1S"; | ||
|
|
||
| void test1(alias a)() | ||
| bool test1(alias a)() | ||
| { | ||
| static struct S {} | ||
| static assert(S.mangleof == result1); | ||
| S s; | ||
| a(&s); // Error: Cannot convert &S to const(S*) at compile time | ||
| return true; | ||
| } | ||
| static assert((test1!f9525(), true)); | ||
| enum evalTest1 = test1!f9525(); | ||
|
|
||
| void test2(alias a)() pure nothrow | ||
| bool test2(alias a)() pure nothrow | ||
| { | ||
| static struct S {} | ||
| static assert(S.mangleof == result2); | ||
| S s; | ||
| a(&s); // Error: Cannot convert &S to const(S*) at compile time | ||
| return true; | ||
| } | ||
| static assert((test2!f9525(), true)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. |
||
| enum evalTest2 = test2!f9525(); | ||
| } | ||
|
|
||
| /******************************************/ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please include an example that now fails to compile and show how to correct it. Perhaps include an example showing how the comma expression can still be used without using the result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was hoping I could avoid the redundancies from https://dlang.org/deprecate.html#Using%20the%20result%20of%20a%20comma%20expression, but okay copying doesn't cost us much.
I copied the text I have written for dlang.org a while ago.