If I remember rightly, there is the example:
a = {-1, -1}
x = 0
a[++x] = x
print(a[0] + " " + a[1])
Depending on the language (and sometimes even if you have optimizations set for those that compile to binaries), you'll get a different answer.I always love (read sarcasm) to see awesome code, such as:
z = a & b > c | d
or: z = a > b ? c < d ? e : f : g ? h : i
And then of course, the statement: "my code doesn't need comments because the code comments itself"...And I think he's agreeing with you, he's just sarcastic.
C does not support his first example. The standard states that the behavior is undefined.
You're probably not wrong, but I specifically remember there was something you could do in order to trick the compiler to allow modification on the left hand side to happen. It could possibly be by having two variable names referencing the same variable - for example.
Besides, the whole point was that it's usually not clever "trying to be clever". It just creates confusion. I don't personally believe the majority of people need to test the entire C spec to it's absolute boundary. A subset serves most people perfectly well.
That's stylistically ugly but not hard to predict; at some point I had it beaten into my head that ternaries are always at the bottom of the single-character operator precedence stack (I really hope all languages stick to that). I'd expect most people to know this.
> z = a & b > c | d
That is harder to predict. I wouldn't expect most people to know this one without looking up a precedence list.
Also there is none of the code golf in there that you have just given as an example.
To expand your example a bit:
class ShoutyContainer:
def __setitem__(self, index, value):
print("Assignment")
def lhs(): print("LHS")
def rhs(): print("RHS")
a = ShoutyContainer()
a[lhs()] = rhs()
Yields: RHS
LHS
Assignment
If LHS were evaluated first, then it would be separated from the final assignment step by the RHS evaluation. That would be odd.You might call this code snippet analogous:
class ShoutyNumber:
def __init__(self, n):
self.n = n
print(f'Created {self.n}')
def __add__(self, other):
print(f'Added with {self.n}')
return self.n + other.n
ShoutyNumber(1) + ShoutyNumber(2)
Yields: Created 1
Created 2
Added with 1
But I understand that in some mental models and some implementations, it's cleaner to have the entire RHS value ready before starting any evaluation of the LHS. I think that avoids the need to have "assignable reference" as a special type of expression result.
LHS first: JavaScript, Java, Go, C#, Swift, PHP, Ruby
RHS first: C++, Python, Rust
I ran this sort of code for all of them:
"=" is syntactically just a binary operator, so I expect it to behave like other binary operators (and AFAIK all other binary operators evaluate left-to-right in almost all languages). It's special because the LHS evaluates to an assignable reference rather than to a value, but nothing stops you from evaluating the left side in full before starting to evaluate the right side. As with every binary operator, it's possible to write code such that the evaluation of one side affects the result of evaluating the other side, but of course that sort of code is really fragile anyway.