digitalmars.D - "++" vs "+=" on function parameter
- claptrap (30/30) Mar 07 ```
- Timon Gehr (3/37) Mar 07 `++p` and `p++` are different. If you want to match `p+=1` you should go...
- claptrap (6/44) Mar 07 I wasn't using pre increment, I was using post, I just mistakenly
- user1234 (6/10) Mar 08 1. This would break code
- claptrap (5/15) Mar 08 agreed
- user1234 (22/38) Mar 08 No. The PostIncr yields a load (so a **rvalue**) that's performed
- Nick Treleaven (7/9) Mar 08 From https://dlang.org/spec/expression.html#order-increment:
- monkyyy (4/6) Mar 09 The inconsistency is with left handed assignment; your doing all
```
module test;
import std.stdio;
void foo(int pos)
{
writeln("inside foo : ",pos);
}
void main()
{
writeln("++");
int p = 0;
foo(p++);
writeln("after foo : ",p);
writeln("+=1");
p = 0;
foo(p+=1);
writeln("after foo : ",p);
}
```
prints:
++
inside foo : 0
after foo : 1
+=1
inside foo : 1
after foo : 1
seems inconsistent to me, probably stuck with it now though I
guess?
probably bad form to use "+=" on a parameter, might be a
candidate for depreciation?
Mar 07
On 3/8/26 03:28, claptrap wrote:
```
module test;
import std.stdio;
void foo(int pos)
{
writeln("inside foo : ",pos);
}
void main()
{
writeln("++");
int p = 0;
foo(p++);
writeln("after foo : ",p);
writeln("+=1");
p = 0;
foo(p+=1);
writeln("after foo : ",p);
}
```
prints:
++
inside foo : 0
after foo : 1
+=1
inside foo : 1
after foo : 1
seems inconsistent to me, probably stuck with it now though I guess?
...
`++p` and `p++` are different. If you want to match `p+=1` you should go
with `++p`.
Mar 07
On Sunday, 8 March 2026 at 03:00:58 UTC, Timon Gehr wrote:On 3/8/26 03:28, claptrap wrote:I wasn't using pre increment, I was using post, I just mistakenly thought (or forgot) p+=2 would be post increment like p++. Just seems like p++ should behave like p+=N for parameters, I mean if you had never seen that syntax before you would probably assume similar behaviour.``` module test; import std.stdio; void foo(int pos) { writeln("inside foo : ",pos); } void main() { writeln("++"); int p = 0; foo(p++); writeln("after foo : ",p); writeln("+=1"); p = 0; foo(p+=1); writeln("after foo : ",p); } ``` prints: ++ inside foo : 0 after foo : 1 +=1 inside foo : 1 after foo : 1 seems inconsistent to me, probably stuck with it now though I guess? ...`++p` and `p++` are different. If you want to match `p+=1` you should go with `++p`.
Mar 07
On Sunday, 8 March 2026 at 03:30:55 UTC, claptrap wrote:[...] Just seems like p++ should behave like p+=N for parameters, I mean if you had never seen that syntax before you would probably assume similar behaviour.1. This would break code 2. that would be inconsistent with the C version 3. that would be globally inconsistent with the definition, that can be reduced to "return the value of the sub-expression before the side-effect".
Mar 08
On Sunday, 8 March 2026 at 10:16:56 UTC, user1234 wrote:On Sunday, 8 March 2026 at 03:30:55 UTC, claptrap wrote:agreed[...] Just seems like p++ should behave like p+=N for parameters, I mean if you had never seen that syntax before you would probably assume similar behaviour.1. This would break code2. that would be inconsistent with the C versionagreed3. that would be globally inconsistent with the definition, that can be reduced to "return the value of the sub-expression before the side-effect".Doesnt that mean "p++" is inconsistent? Arn't "p++" and "p+=1" essentially shorthand for "p=p+1"
Mar 08
On Sunday, 8 March 2026 at 10:26:54 UTC, claptrap wrote:On Sunday, 8 March 2026 at 10:16:56 UTC, user1234 wrote:No. The PostIncr yields a load (so a **rvalue**) that's performed before the side-effect. The AddAssign version yields a **lvalue**. So using a pseudo IR, postIncr is: ``` %0 = ... ; let's say it's a global or an alloca, so a pointer %1 = load i32 %0 ; rvalue before the side effect %2 = add %1, i32 1 ; new rvalue store %0, %2 ; store to the lvalue return %1 ; before the side effect ``` while AddAssign is ``` %0 = ... %1 = load i32 %0 %2 = add %1, 1 store %0, %2 return %0 ; return the lvalue ``` That's essentially why the post-inc/dec cant be chained (after the first you cant load anymore).On Sunday, 8 March 2026 at 03:30:55 UTC, claptrap wrote:agreed[...] Just seems like p++ should behave like p+=N for parameters, I mean if you had never seen that syntax before you would probably assume similar behaviour.1. This would break code2. that would be inconsistent with the C versionagreed3. that would be globally inconsistent with the definition, that can be reduced to "return the value of the sub-expression before the side-effect".Doesnt that mean "p++" is inconsistent? Arn't "p++" and "p+=1" essentially shorthand for "p=p+1"
Mar 08
On Sunday, 8 March 2026 at 10:26:54 UTC, claptrap wrote:Doesnt that mean "p++" is inconsistent? Arn't "p++" and "p+=1" essentially shorthand for "p=p+1"From https://dlang.org/spec/expression.html#order-increment: ```d Expression Equivalent ++expr ((expr) += 1) expr++ (ref x){auto t = x; ++x; return t;}(expr) ```
Mar 08
On Sunday, 8 March 2026 at 12:42:19 UTC, Nick Treleaven wrote:On Sunday, 8 March 2026 at 10:26:54 UTC, claptrap wrote:I'm amazed how we have the whole discussion about obvious thing that every high school student who ever learned any C-like language knows :)Doesnt that mean "p++" is inconsistent? Arn't "p++" and "p+=1" essentially shorthand for "p=p+1"From https://dlang.org/spec/expression.html#order-increment: ```d Expression Equivalent ++expr ((expr) += 1) expr++ (ref x){auto t = x; ++x; return t;}(expr) ```
Mar 08
On Sunday, 8 March 2026 at 12:50:35 UTC, Sergey wrote:On Sunday, 8 March 2026 at 12:42:19 UTC, Nick Treleaven wrote:that's not necessarily a high schooler thing. The point is "do you need the rvalue before the side effect". There is an interesting discussion here https://softwareengineering.stackexchange.com/questions/43067/why-do-we-have-postfix-increment So apparently the postfix version matches to an instruction in the PDP arch ? cool. However even with the common x86 arch you're pretty sure that the result will stand in a register, which opens doors to optimz. But seriously ```d (ref x){auto t = x; ++x; return t;}(expr) ``` does not express well that, i.e the "value category" (boring topic)On Sunday, 8 March 2026 at 10:26:54 UTC, claptrap wrote:I'm amazed how we have the whole discussion about obvious thing that every high school student who ever learned any C-like language knows :)Doesnt that mean "p++" is inconsistent? Arn't "p++" and "p+=1" essentially shorthand for "p=p+1"From https://dlang.org/spec/expression.html#order-increment: ```d Expression Equivalent ++expr ((expr) += 1) expr++ (ref x){auto t = x; ++x; return t;}(expr) ```
Mar 09
On Sunday, 8 March 2026 at 02:28:31 UTC, claptrap wrote:seems inconsistent to me, probably stuck with it now though I guess?The inconsistency is with left handed assignment; your doing all this math with the right hand of the ='s then you swap back the the left hand to commit it
Mar 09









user1234 <user1234 12.de> 