Warn about do nothing expressions?

QAston qaston at gmail.com
Fri Mar 28 11:29:26 PDT 2014


On Friday, 28 March 2014 at 18:04:41 UTC, Frustrated wrote:
> On Friday, 28 March 2014 at 16:54:49 UTC, Benjamin Thaut wrote:
>> I had a bug which came down to the following line today:
>>
>> m_takeIndex = m_takeIndex++;
>>
>> Actually this line does nothing. m_takeIndex remains at 
>> exactly the same value as before (tested with dmd 2.065).
>>
>> Can someone please explain why? And would be suitable to warn 
>> about this, if it is correct behaviour?
>>
>> Kind Regards
>> Benjamin Thaut
>
> This should be invalid.
>
> m_takeIndex++;
>
> actually increments the variable.
>
> m_takeIndex = m_takeIndex++; should do exactly the same.
>
> It is a bug. It might be correct in monarch's world but it is 
> not logical. I think monarch is just trying to justify the way 
> D does it, regardless if D is D is wrong or not.
>
> I imagine what is going on is that D is creating a temp 
> variable, assigning it to the local variable, then incrementing 
> the temp variable(since++ increments "after" the assignment).
>
> That, or D really is treating that as a nop, which is wrong too.
>
> Test case shows it is probably the first:
>
> 	int j = 0, i = 0;
> 	i = j++;
> 	// i = 0, j = 1
>
> hence D is wrong, Monarch is wrong, etc...
>
> m_takeIndex = m_takeIndex++;
>
> should, given the example above, evaluate to
>
> m_takeIndex = m_takeIndex; (a nop)
> m_takeIndex++; (variable incremented)
>
>
> hence, D is not consistent with itself, which at the very least 
> is a bug.

Monarch is right, expression value of i++ is the old i value. 
Right side of = is evaluated first, so i is incremented and then 
= is evaluated so it gets it's old value back. This is consistent 
with the rest of the language, your proposal is a special case.


More information about the Digitalmars-d mailing list