Assigning variable values "on the fly" within expressions

Click For Summary

Discussion Overview

The discussion centers around the concept of assigning variable values "on the fly" within expressions across different programming languages, including JavaScript, Mathematica, C++, and APL. Participants explore the syntax, implications, and potential pitfalls of using such expressions in various coding environments.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a syntax example in JavaScript and Mathematica, questioning its terminology.
  • Another participant refers to the term "assignment expression" and discusses the potential drawbacks of using such expressions, including obscured code and optimization issues.
  • A participant shares a C++ code example demonstrating the use of assignment expressions, noting the output produced.
  • Another participant reiterates the C++ example but emphasizes that the order of operations is not guaranteed, which could lead to unexpected results.
  • A participant stresses the importance of understanding language specifications regarding order of evaluation when using assignment expressions.
  • A participant introduces APL, explaining how variable assignment works within the context of order of evaluation, providing an example that illustrates this behavior.

Areas of Agreement / Disagreement

Participants express varying opinions on the use of assignment expressions, with some cautioning against their use due to potential issues, while others provide examples of their functionality in different languages. The discussion remains unresolved regarding the overall merits and risks of using such syntax.

Contextual Notes

Participants highlight limitations related to the order of evaluation in different programming languages, indicating that behavior may vary and could lead to subtle errors if not properly understood.

Swamp Thing
Insights Author
Messages
1,047
Reaction score
786
TL;DR
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   Reactions: 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   Reactions: 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!
 
  • Haha
Likes   Reactions: Nugatory
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
 

Similar threads

Replies
235
Views
14K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
4
Views
3K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 40 ·
2
Replies
40
Views
8K