agileobjects/ReadableExpressions

Increment and Decrement expressions are disassembled incorrectly

Closed this issue · 4 comments

The Increment and Decrement expression types are diassembled like pre-increment/pre-decrement operators, but these operators are really only the equivalent of operand + 1 and operand - 1.

This expression:

var x =
    Expression.Variable(typeof(byte), "x");
        Expression.Assign(
            x,
            Expression.Convert(
	        Expression.Increment(
	            Expression.Convert(x, typeof(int))
	        ),
	    typeof(byte)
        )
    )
    .ToReadableString()
    .Dump();

outputs x = (byte)++(int)x, when x = (byte)((int) + 1) is the correct interpretation.

In idiomatic C#, this is x += 1 for a variable of type byte.

Hi,

Thanks for the feedback!

Looking at the documentation for Expression.Increment() (and the documentation for Decrement() is basically the same), and given that Expression has separate PreIncrement() / PostIncrement() / PreDecrement() / PostDecrement() factory methods which represent (and translate to) the pre and post ++ / -- operators, maybe Increment() and Decrement() should be translated to x + 1 / x - 1?

In fact the code sample includes a comment stating "The value of the variable did not change, because the expression is functional", which pretty much makes it explicit.

What d'you think?

Cheers,

Steve

This is fixed in the v3.0 branch, which will be releasing in the next week or so.

Thanks again!

This fix is part of the v3.0-preview1 package, which is now on NuGet.

Thanks!

This is part of the full v3 release, which is now available on NuGet. Cheers!