<p>In C-style languages we have:</p>
<pre><code>int x = 10;
x++; // x is now 11 but the expression returns 10
</code></pre>
<pre><code>int x = 10;
++x; // x is now 11 and the expression returns 11
</code></pre>
<pre><code>int x = 10;
x += 2; // x is now 12 and the expression returns 12
</code></pre>
<p>But we don't have:</p>
<pre><code>int x = 10;
x ??? 2; // x is now 12 but the expression returns 10
</code></pre>
<p>Why would it make sense to allow an inline operation to return a value before adding a value, but only if the value you are adding is one? What are some syntax options to implement a post-augmented assignment operator, that evaluates to the <strong>old</strong> value of the left-hand operand?</p>

<syntax><operators>
Pre-increment and post-increment and augmented assignment but no post-augmented assignment?
<p>Three things of historical note:</p>
<ul>
<li>C was designed so that each operation closely corresponds to a hardware instruction.</li>
<li>C got its <code>++</code> and <code>--</code> operators from the PDP-11 instruction set (deferred increment, and deferred decrement).</li>
<li>C allows assignment statements to be treated as expressions, since the instruction has already left the value available in a register.</li>
</ul>
<p>There is no efficient or natural path from hardware instructions to the proposed operator (except for the case of adding or subtracting exactly 1), so there would have been no reason to invent such an operator.</p>
<p>In practice, the equivalent expression <code>y = ((x+=n), (x-n))</code>, where <code>n</code> is not 1 or -1, almost never occurs anywhere, so there is no demand for inventing syntactic sugar for it.<br />
Even if it <em>were</em> common, it would be no more efficient than <code>y = x ; x += 2</code> or <code>((x += 2), (y = x - 2))</code>.</p>

CC BY-SA 4.0
2023-06-14T02:00:15.280Z
2023-06-14T02:05:43.450Z
2023-06-14T02:05:43.450Z
Ray Butterworth
<p>In some languages, such as Swift, assignment operators don't return anything in the first place. It's not super common to use an assignment (even just <code>=</code>) as an expression; I've typically seen it for two things:</p>
<ol>
<li>Chained assignment, <code>a = b = c</code></li>
<li>Assignment and null-check, <code>if ((ptr = getThing())) ...</code></li>
</ol>
<p>The compound assignment operators like <code>+=</code> aren't conducive to these uses anyways. I don't think I've ever intentionally used the return value of a compound assignment operator.</p>

CC BY-SA 4.0
2023-06-13T23:07:32.997Z
2023-06-13T23:07:32.997Z
Bbrk24
<blockquote>
<p>Why would it make sense to allow an inline operation to return a value before adding a value, but only if the value you are adding is one?</p>
</blockquote>
<p>One significant use-case is writing to an array or buffer: you want to store a value at the current index, while incrementing the index, but the value should be stored at the <em>old</em> index.</p>
<pre class="lang-c prettyprint-override"><code>arr[n++] = something;
</code></pre>
<p>This is preferable to the alternative of pre-incrementing the index, for two reasons: firstly because it allows the index <code>n</code> to be initialised as 0 instead of −1 (zero is more natural, and the index is often an unsigned integer), and secondly because if <code>n</code> is always the <em>next</em> index to be used then it happens to also equal the total number of indices in use, i.e. the length of the sequence.</p>
<p>Likewise, another significant use-case is assigning unique, incrementing IDs to some objects. The arguments in favour of post-incrementing over pre-incrementing are the same. In both use-cases, what matters is that the variable ranges over consecutive increasing integers, so naturally the number you want to add is 1.</p>
<blockquote>
<p>What are some syntax options to implement a post-augmented assignment operator, that evaluates to the <strong>old</strong> value of the left-hand operand?</p>
</blockquote>
<p>I don't know of any popular languages which have a syntax for this, and I doubt there are any; it seems too niche to be worth having a dedicated syntax for. (The most useful case might actually be for regular assignment, instead of augmented assignment.) So if you do want to add it to your language, there's probably no good precedent to borrow syntax from.</p>
<p>One option would be to use some special symbol to denote that an assignment should evaluate with the old value; something like <code>x += 5</code> could be a pre-add-assign, while <code>x @+= 5</code>, <code>x $+= 5</code> or <code>x #+= 5</code> could be a post-add-assign. The problem with this approach is that it won't make sense to readers who aren't familiar with the language, and it will be hard to search for in order to find out (e.g. Google search doesn't handle punctuation).</p>
<p>Another option which would be possible in some languages would be to make the operation as a function or method, so that it has a name. This would work in languages where a function can accept a reference to a variable, so that it can mutate the variable via the reference. For example, in Rust, it's even possible for the user to add this behaviour to the built-in numeric types:</p>
<pre class="lang-rust prettyprint-override"><code>trait PostAddAssign&lt;Rhs = Self&gt; {
    fn post_add(&amp;mut self, rhs: Rhs) -&gt; Self;
}

impl &lt;T, Rhs&gt; PostAddAssign&lt;Rhs&gt; for T
where T: std::ops::AddAssign&lt;Rhs&gt; + Copy {
    fn post_add(&amp;mut self, rhs: Rhs) -&gt; Self {
        let old = *self;
        *self += rhs;
        old
    }
}
</code></pre>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=d57a290810811c8dc5bfa73de30c8f6b" rel="nofollow noreferrer">Rust playground link</a></p>

CC BY-SA 4.0
2023-06-13T22:24:22.437Z
2023-06-13T23:26:06.703Z
2023-06-13T23:26:06.703Z
kaya3
CC BY-SA 4.0
2023-06-14T03:16:59.143Z
As for the syntax, maybe something like `5 =+ x`?
Dannyu NDos
CC BY-SA 4.0
2023-06-14T23:39:23.957Z
@DannyuNDos Early versions of C did that for modern `+=`, or so I've heard, but the problem becomes that `x=-10` is ambiguous -- `x = -10` or `x =- 10`?
Bbrk24
CC BY-SA 4.0
2023-06-14T23:55:09.457Z
Another issue with just using a mirror-image is that doesn't work for basic assignment like `x = y`, which is probably the most useful case (i.e. assign a new value to `x` and return the old value).
kaya3
# Pre-increment and post-increment and augmented assignment but no post-augmented assignment?
Posted by: username_0 on 2023-06-13
In C-style languages we have:
int x = 10;
x++; // x is now 11 but the expression returns 10

int x = 10;
++x; // x is now 11 and the expression returns 11

int x = 10;
x += 2; // x is now 12 and the expression returns 12

But we don't have:
int x = 10;
x ??? 2; // x is now 12 but the expression returns 10

Why would it make sense to allow an inline operation to return a value before adding a value, but only if the value you are adding is one? What are some syntax options to implement a post-augmented assignment operator, that evaluates to the old value of the left-hand operand?
**Tags:** syntax operators
---
## 3 answers
### Answer by username_1 on 2023-06-14. Score: 6
Three things of historical note:

* C was designed so that each operation closely corresponds to a hardware instruction.
* C got its `++` and `--` operators from the PDP-11 instruction set (deferred increment, and deferred decrement).
* C allows assignment statements to be treated as expressions, since the instruction has already left the value available in a register.

There is no efficient or natural path from hardware instructions to the proposed operator (except for the case of adding or subtracting exactly 1), so there would have been no reason to invent such an operator.

In practice, the equivalent expression `y = ((x+=n), (x-n))`, where `n` is not 1 or -1, almost never occurs anywhere, so there is no demand for inventing syntactic sugar for it.  
Even if it *were* common, it would be no more efficient than `y = x ; x += 2` or `((x += 2), (y = x - 2))`.
---
### Answer by username_2 on 2023-06-13. Score: 2
In some languages, such as Swift, assignment operators don't return anything in the first place. It's not super common to use an assignment (even just `=`) as an expression; I've typically seen it for two things:

1. Chained assignment, `a = b = c`
2. Assignment and null-check, `if ((ptr = getThing())) ...`

The compound assignment operators like `+=` aren't conducive to these uses anyways. I don't think I've ever intentionally used the return value of a compound assignment operator.
---
### Answer by username_3 on 2023-06-13. Score: 1
Why would it make sense to allow an inline operation to return a value before adding a value, but only if the value you are adding is one?

One significant use-case is writing to an array or buffer: you want to store a value at the current index, while incrementing the index, but the value should be stored at the old index.
arr[n++] = something;

This is preferable to the alternative of pre-incrementing the index, for two reasons: firstly because it allows the index n to be initialised as 0 instead of −1 (zero is more natural, and the index is often an unsigned integer), and secondly because if n is always the next index to be used then it happens to also equal the total number of indices in use, i.e. the length of the sequence.
Likewise, another significant use-case is assigning unique, incrementing IDs to some objects. The arguments in favour of post-incrementing over pre-incrementing are the same. In both use-cases, what matters is that the variable ranges over consecutive increasing integers, so naturally the number you want to add is 1.

What are some syntax options to implement a post-augmented assignment operator, that evaluates to the old value of the left-hand operand?

I don't know of any popular languages which have a syntax for this, and I doubt there are any; it seems too niche to be worth having a dedicated syntax for. (The most useful case might actually be for regular assignment, instead of augmented assignment.) So if you do want to add it to your language, there's probably no good precedent to borrow syntax from.
One option would be to use some special symbol to denote that an assignment should evaluate with the old value; something like x += 5 could be a pre-add-assign, while x @+= 5, x $+= 5 or x #+= 5 could be a post-add-assign. The problem with this approach is that it won't make sense to readers who aren't familiar with the language, and it will be hard to search for in order to find out (e.g. Google search doesn't handle punctuation).
Another option which would be possible in some languages would be to make the operation as a function or method, so that it has a name. This would work in languages where a function can accept a reference to a variable, so that it can mutate the variable via the reference. For example, in Rust, it's even possible for the user to add this behaviour to the built-in numeric types:
trait PostAddAssign {
    fn post_add(&mut self, rhs: Rhs) -> Self;
}

impl  PostAddAssign for T
where T: std::ops::AddAssign + Copy {
    fn post_add(&mut self, rhs: Rhs) -> Self {
        let old = *self;
        *self += rhs;
        old
    }
}

Rust playground link
> **Comments:**
> **username_4** (2023-06-14): As for the syntax, maybe something like `5 =+ x`?  
> **username_2** (2023-06-14): @DannyuNDos Early versions of C did that for modern `+=`, or so I've heard, but the problem becomes that `x=-10` is ambiguous -- `x = -10` or `x =- 10`?  
> **username_3** (2023-06-14): Another issue with just using a mirror-image is that doesn't work for basic assignment like `x = y`, which is probably the most useful case (i.e. assign a new value to `x` and return the old value).  
---
