-
-
Notifications
You must be signed in to change notification settings - Fork 379
Clarify spec for "+=" and friends #1429
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
base: master
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -96,10 +96,38 @@ $(H4 Assignment Operator Expressions) | |
$(P except that:) | ||
|
||
$(UL | ||
$(LI operand $(D a) is only evaluated once) | ||
$(LI operand $(D a) is only evaluated once, see below.) | ||
$(LI overloading $(I op) uses a different function than overloading $(I op)= does) | ||
$(LI the left operand of $(D >>>=) does not undergo integral promotions before shifting) | ||
) | ||
|
||
$(P The expression evaluates according to | ||
|
||
-------------- | ||
void doOpAssignment(ref int x, int y) | ||
{ | ||
x = x op y; | ||
} | ||
doOpAssignment(a, b); | ||
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. The description needs to include the cast. I'd say a correct formulation goes as follows:
See also (sadly) https://issues.dlang.org/show_bug.cgi?id=16271 which blocks this. Other than that this definition is really solid because defines everything by "redirecting" to function calls. 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. |
||
-------------- | ||
|
||
thus the following unittest passes | ||
|
||
-------------- | ||
int sum; | ||
int add7tosum_return1() { sum += 7; return 1; } | ||
unittest | ||
{ | ||
sum = 0; | ||
sum += add7tosum_return1(); | ||
assert(sum == 8); // "eight" ! | ||
|
||
sum = 0; | ||
sum = cast(typeof(sum))(sum + add7tosum_return1()); | ||
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. Why is the cast necessary? 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. It isn't. But I made it look exactly the same as the given "semantic equivalent" above. 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. It is necessary if all isn't int. |
||
assert(sum == 1); // "one" ! | ||
} | ||
-------------- | ||
) | ||
|
||
$(H3 Conditional Expressions) | ||
|
||
|
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.
There is a cast missing in there. I did not put it in the forum thread as I used int explicitly, but it is needed in the general case. For instance :
byte a = 3;
a += 3; // This is ok because of the cast, but won't work because of int promotion without).