Re: OT: and even in Dart .........Re: simple session question

On 10/22/2011 4:08 PM, Norman Peelman wrote:
On 10/22/2011 01:34 PM, Thomas Mlynarczyk wrote:
Luuk schrieb:
i did send a bug-report:

I do not see any bug here. I was confused because it's a crappy way to
code, but it's clear why it works the way it does:

$foo = 0;
$foo = $foo++;

is definitely identical to

$foo = 0;
$foo = ($foo++);

since ++ has higher precedence than =. In fact, ($foo = $foo)++ does
rightfully throw a parse error, since you can only increment a variable,
not an expression.

As mentioned before in this thread, the above code is equivalent to

$foo = 0;
$tmp = $foo; // $foo++ yields the previous value, which is 0
$foo = $foo + 1; // then $foo is incremented...
$foo = $tmp; // ...and then re-assigned the old value

It's the exact same procedure as it would be with $foo = $bar++.


No they are not the same.

$foo = 0;
$foo = $foo++;

It should be equivalent to:

$foo = 0; // 0
$foo = $foo; // 0 = 0
$foo = $foo + 1; // 0 = (0 + 1)

$foo++ means that the variable is to be incremented after the variable
is accessed. Something is clobbering that increment.

++$foo means that the variable is to be incremented piror to the
variable being accessed. Works as expected.

Haven't seen the source code but there must be some temporary variable
that's getting clobbered for the $foo++ example.

Actually, I take my previous statement about the behavior being defined back. Officially in C/C++, the results of this operation is undefined.

Precedence and associativity define the order in which operators are processed. But the order of operand processing is not defined.


$foo = $foo++;

we have the same operand ($foo) being set twice. The $foo++ will return 0, and this value will be assigned into $foo.

But what is NOT defined is whether the assignment will occur before or after the value of $foo is actually incremented. Either is possible.

Now in the case of

$foo = ($foo++);

The results are different, but they are still undefined.

In the first case the increment is obviously performed before the assignment (although the assignment correctly uses the pre-increment value).

In the second case it looks like the assignment is still using the pre-increment value, but the increment is done after the assignment.

Since the results are undefined, both are correct (or incorrect, as you may look at it), and may change with changes to the interpreter (or potentially the same version on different OS's).

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.