Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Post Assign

Name: OP !9kbJjvwmwE 2012-12-27 14:19

I find post/pre-increment/decrement very, very useful (I'm refering to x++, ++x etc.). Often it's what saves me from having to declare redundant variables or, god forbid, redundant functions.
The time has come in which I wonder whether this kind of magic is possible with assignment as well (in any language).
In the following JavaScript code, the expression in the parentheses returns the value assigned to the variable x (or the value of x after it had been assigned the new value):
(x = 1)
Is there a language that provides a way to write a similar expression which will return the value of x *before* it's assigned the new value?
An imaginary example would be:
var x = "before";
var y = (x = "after");

in which y will be assigned the value "before" (instead of "after", as it would if this was run as JavaScript).

Better yet, in a language that does *not* provide a native syntax to achieve this, can you think of a hack that do it? For example, in JavaScript, the best way I can think of to achieve this without declaring additional variables is:
var x = "bef";
var y = [x, x = "aft"][0];

Here in the second line I declare an anonymous Array with the first element set to x and the second element set to the result of the expression setting x to the new value, and then retrieve the first element of that array (the second element is irrelevant here) and assign it to y.

In the next post I will explain why such a procedure would ever be useful for anything (which you are likely to ask).

Name: Anonymous 2012-12-27 15:15

>>4
You have yet to achieve satori. Please leave and do not return until you have. Also learn Sympta.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List