digitalmars.D - Any reason why ++1 is not folded to a new constant?
- Basile B. (12/12) Oct 20 2020 I've reached a similar problem in another language and I wanted
- Basile B. (8/20) Oct 20 2020 well my first guess is that it's because it's lowered to a binass
- Walter Bright (5/16) Oct 21 2020 Because ++ is supposed to operate on an lvalue, and `1` is an rvalue. `+...
- mw (4/8) Oct 21 2020 👍, well said.
- Patrick Schluter (6/23) Oct 22 2020 The question is then: why did it compile?
- Mathias LANG (3/7) Oct 22 2020 It doesn't ? https://run.dlang.io/is/sk4kKw
I've reached a similar problem in another language and I wanted to see what is the D policy. I've been surprised by the result. It seems that there's no special case for compile-time-only values, eg this case of RValue: --- void main() { writeln(++1); // NG: cannot modify constant `1` } --- is there any reasons why ++1 is not optimized to 2 ? Thanks for the enlightment.
Oct 20 2020
On Tuesday, 20 October 2020 at 12:41:48 UTC, Basile B. wrote:I've reached a similar problem in another language and I wanted to see what is the D policy. I've been surprised by the result. It seems that there's no special case for compile-time-only values, eg this case of RValue: --- void main() { writeln(++1); // NG: cannot modify constant `1` } --- is there any reasons why ++1 is not optimized to 2 ? Thanks for the enlightment.well my first guess is that it's because it's lowered to a binass operation before optimization. when you think that the expression optimizer sees `writeln(++1)` actually it sees `writeln(1 += 1)` hence the error...
Oct 20 2020
On 10/20/2020 5:41 AM, Basile B. wrote:I've reached a similar problem in another language and I wanted to see what is the D policy. I've been surprised by the result. It seems that there's no special case for compile-time-only values, eg this case of RValue: --- void main() { writeln(++1); // NG: cannot modify constant `1` } --- is there any reasons why ++1 is not optimized to 2 ?Because ++ is supposed to operate on an lvalue, and `1` is an rvalue. `++1` is nonsense. I don't see the point to adding a special case for it - special cases are warts and need strong justifications to add.
Oct 21 2020
On Wednesday, 21 October 2020 at 21:49:11 UTC, Walter Bright wrote:Because ++ is supposed to operate on an lvalue, and `1` is an rvalue. `++1` is nonsense. I don't see the point to adding a special case for it - special cases are warts and need strong justifications to add.👍, well said. I hope every D language feature has no special cases.
Oct 21 2020
On Wednesday, 21 October 2020 at 21:49:11 UTC, Walter Bright wrote:On 10/20/2020 5:41 AM, Basile B. wrote:The question is then: why did it compile? gcc gives error: lvalue required as increment operand when one tries it in C.I've reached a similar problem in another language and I wanted to see what is the D policy. I've been surprised by the result. It seems that there's no special case for compile-time-only values, eg this case of RValue: --- void main() { writeln(++1); // NG: cannot modify constant `1` } --- is there any reasons why ++1 is not optimized to 2 ?Because ++ is supposed to operate on an lvalue, and `1` is an rvalue. `++1` is nonsense. I don't see the point to adding a special case for it - special cases are warts and need strong justifications to add.
Oct 22 2020
On Thursday, 22 October 2020 at 09:49:32 UTC, Patrick Schluter wrote:The question is then: why did it compile? gcc gives error: lvalue required as increment operand when one tries it in C.It doesn't ? https://run.dlang.io/is/sk4kKw
Oct 22 2020