Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't an expression tree contain a named argument specification?

Using AutoMapper, I hit a place where a named argument would've fit very nicely:

.ForMember(s => s.MyProperty, opt => opt.MapFrom(s => BuildMyProperty(s, isAdvanced: false))) 

But the compiler yelled at me:

An expression tree may not contain a named argument specification

So I had to revert to:

.ForMember(s => s.MyProperty, opt => opt.MapFrom(s => BuildMyProperty(s, false))) 

Does anyone know why the compiler disallows named arguments in this situation?

like image 817
Brandon Linton Avatar asked Apr 12 '12 21:04

Brandon Linton


People also ask

Why would you use an expression tree?

When you want to have a richer interaction, you need to use Expression Trees. Expression Trees represent code as a structure that you can examine, modify, or execute. These tools give you the power to manipulate code during run time. You can write code that examines running algorithms, or injects new capabilities.

How does an expression tree work?

Expression trees represent code in a tree-like data structure, where each node is an expression, for example, a method call or a binary operation such as x < y . You can compile and run code represented by expression trees.

What is query expression trees?

Expression tree is an in-memory representation of a lambda expression. It holds the actual elements of the query, not the result of the query. The expression tree makes the structure of the lambda expression transparent and explicit.


1 Answers

Consider the following:

static int M() { Console.Write("M"); return 1; } static int N() { Console.Write("N"); return 2; } static int Q(int m, int n) { return m + n; } ... Func<int> f = ()=>Q(n : N(), m: M()); Expression<Func<int>> x = ()=>Q(n : N(), m: M()); Func<int> fx = x.Compile(); Console.WriteLine(f()); Console.WriteLine(fx()); 

You agree I hope that the last two lines must do exactly the same thing, right? Which is to print NM3.

Now, what expression tree library calls would you like the expression tree conversion to generate that ensure this? There are none! We are therefore faced with the following choices:

  1. Implement the feature in the expression tree library. Add a transformation in the expression tree lowering engine that preserves the order of execution of the named arguments. Implement code in the Compile method that takes the execution order into account.
  2. Make x = ()=>Q(n : N(), m: M()); actually be implemented as x = ()=>Q(M(), N()); and be incompatible with the non-expression-tree version.
  3. Disallow named arguments in expression trees. Implement an error message to that effect.

(1) is nice, but expensive. (2) is a non-starter; we can't in good conscience introduce this kind of "gotcha". (3) is cheap but irritating.

We chose (3).

like image 157
Eric Lippert Avatar answered Sep 21 '22 14:09

Eric Lippert