1

From the documentation, and its name implying, it can be inferred that the value of the CanReduce property must be set to true for all expressions that can further be decomposed into smaller expressions, and vice-versa.

But upon closer observation, this inference appears not to hold true in all cases. Take the case of LambdaExpression, which certainly is a composite unit. But the LambdaExpression class, deriving directly from the Expression class, does not override the CanReduce property. The Expression class defines the CanReduce property as virtual with an implementation that returns false, thus implying that a lambda expression is not further reducible, which is not true.

What then is the real meaning of this property?

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • 2
    For example, compound assignment `a+=b` can be reduced to `a=a+b`. – user4003407 Apr 24 '15 at 12:26
  • Nice. Thank you. Is the meaning restricted to just these operators (I forget what they're called)? – Water Cooler v2 Apr 24 '15 at 12:27
  • I does not dig that deep into `System.Linq.Expressions`, to say is it true or not. – user4003407 Apr 24 '15 at 12:32
  • Thank you very much, nevertheless. I think I found something that might be useful in this case, over here: http://stackoverflow.com/a/2044367/303685 – Water Cooler v2 Apr 24 '15 at 12:36
  • possible duplicate of [.NET 4.0: What does Expression.Reduce() do?](http://stackoverflow.com/questions/2038759/net-4-0-what-does-expression-reduce-do) (I know, you are asking about `CanReduce`, not `Reduce`. However if you understand what the latter does, you will be a very large step closer to understanding the former.) – stakx - no longer contributing Apr 24 '15 at 22:37

2 Answers2

5

I posted a longer answer here with more details: What does Expression.Reduce() do?, but the short form is that the out-of-box .NET behavior seems to only reduce the following scenarios:

  • Compound assignment e.g. x += 4
  • Pre/post increment/decrement e.g. x++, --y
  • Member and list initialization e.g. new List<int>() { 4, 5 }, new Thing() { Prop1 = 4, Prop2 = 5 }

Everything else is left as-is. I didn't see any evidence for or against dead expression culling as part of a reduce operation (e.g. an empty BlockExpression won't get removed from an expression tree during Reduce() calls).

Community
  • 1
  • 1
Joe Friesenhan
  • 432
  • 5
  • 7
1

I think you're reading the documentation wrong. "Reducing" here doesn't mean decomposing into multiple simpler expressions, it means transforming into a single expression that uses more basic operations. For example, consider the following ListInitExpression (using C#-like syntax):

new List<int> { 1, new Random().Next() }

Calling CanReduce on this expression will return true. And calling Reduce() will return:

{
    Param_0 = new List<int>();
    Param_0.Add(1);
    Param_0.Add(new Random().Next());
    return Param_0;
}

It's not clear to me what should Reduce() on a LambdaExpression return, so it makes sense to me that it's not reducible.

svick
  • 236,525
  • 50
  • 385
  • 514