Assigning variable values "on the fly" within expressions

AI Thread Summary
The discussion centers around the use of assignment expressions in programming languages like JavaScript, Mathematica, and C++. The syntax in question allows for variable assignments within expressions, exemplified by the equation z = ((a=3) +( b=2) )**2 + 1/a + 1/b. While this syntax is recognized, particularly as an "assignment expression," its use raises concerns regarding code clarity and potential errors. The conversation highlights that while the expression works in C++, the order of evaluation is not guaranteed, which can lead to unpredictable behavior. The importance of understanding language specifications regarding evaluation order is emphasized, as it can affect the correctness of the code. Additionally, a mention of APL illustrates that variable assignments can be managed differently, allowing for clearer initialization within expressions. Overall, the discussion warns against the risks of using assignment expressions without a thorough understanding of their implications.
Swamp Thing
Insights Author
Messages
1,032
Reaction score
770
TL;DR Summary
This works in JavaScript : ((a=3) +( b=2) )**2 + 1/a +1/b

Is there a term for it? Does it work in may other languages?
I've used this kind of thing often in JavaScript and Mathematica.
Code:
z = ((a=3) +( b=2) )**2 + 1/a +1/b
I'm currently learning Julia, and it doesn't seem to be supported.

What is this syntax called?
 
Technology news on Phys.org
“Assignment expression” is one term sometimes used.

I won’t enter into the debate about whether using assignment expressions is a good idea (basically, whether the unnecessary and irrelevant reduction in line count justifies the gratuitous obscuration of the code, interference with the compiler’s ability to optimize, and potential for introducing subtle errors) but I will say that if you’re going to use them…. Be very sure that you understand what your language specification says about order of evaluation.
 
  • Like
  • Haha
Likes Swamp Thing, jedishrfu, pbuk and 2 others
It works in C++, too.

Code:
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    int a, b;
    int c = ((a = 3) + (b = 2)) * 2;  
    cout << a << ' ' << b << ' ' << c << endl;
    return 0;
}

produces the output

Code:
3 2 10
 
jtbell said:
It works in C++, too.
But note that the more complicated expression in the OP (((a = 3) + (b = 2)) ** 2 + 1/a + 1/b) will compile but there is no guarantee that the compiler will set a to 3 before it calculates 1/a (the order of these operations is not defined in the standard).

So as somebody not quite said upthread, even if this works it is never a good idea.
 
Last edited:
  • Like
Likes phyzguy, Nugatory, Swamp Thing and 1 other person
Nugatory said:
Be very sure that you understand what your language specification says about order of evaluation.
Please forgive me for paraphrasing that liberally!
 
In APL, variable assignment can be done according to the order of evaluation. In this example, the right Z is declared, but uninitialized, where initialization occurs within the parenthesis. The expression is 2 modulo (Z##\leftarrow## 1 2 3 4 5) reduce Z (delete all values where Z modulo 2 are zero).

$$ 2|(Z \leftarrow 1 \ 2 \ 3 \ 4 \ 5)/Z $$

returns 1 3 5
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

Similar threads

Back
Top