Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are lambdas convertible to expressions but method groups are not?

Tags:

c#

lambda

c#-4.0

LINQPad example:

void Main()
{
    One(i => PrintInteger(i));
    One(PrintInteger);

    Two(i => PrintInteger(i));
    // Two(PrintInteger); - won't compile
}

static void One(Action<int> a)
{
    a(1);
}

static void Two(Expression<Action<int>> e)
{
    e.Compile()(2);
}

static void PrintInteger(int i)
{
    Console.WriteLine(i);
}

Uncommenting the Two(PrintInteger); line results in an error:

cannot convert from 'method group' to 'System.Linq.Expressions.Expression<System.Action<int>>'

This is similar to Convert Method Group to Expression, but I'm interested in the "why." I understand that Features cost money, time and effort; I'm wondering if there's a more interesting explanation.

like image 881
TrueWill Avatar asked Apr 07 '13 18:04

TrueWill


People also ask

How do you convert this method into a lambda expression?

To convert an anonymous method to a lambda expressionMove to the anonymous method you want to convert. From the Refactor menu of the VisualAid choose To Lambda. Telerik® JustCode™ will replace the anonymous method with a lambda expression.

What is a method group?

What is Method Group? We're a leading IT services provider based in the New York metropolitan area. Method Group supports small businesses by implementing customized, best-in-class IT solutions that boost efficiency, enhance security and manage technology costs.

What is lambda expression in Linq?

A lambda expression is a convenient way of defining an anonymous (unnamed) function that can be passed around as a variable or as a parameter to a method call. Many LINQ methods take a function (called a delegate) as a parameter.

Which sentence about lambda statements is not true?

Which is NOT true about lambda statements? A statement lambda cannot return a value.


2 Answers

There is no reason in principle. It could be done this way. The compiler could just create the lambda by itself before converting it (this is obviously always possible - it knows the exact method being called so it can just create a lambda from its parameters).

There is one catch, though. The name of the parameter of your lambda is normally hard-coded into the IL being generated. If there is no lambda, there is no name. But the compiler could either create a dummy name or reuse the names of the method being called (they are always available in the .NET assembly format).

Why didn't the C# team decide to enable this? The only reason that comes to mind is that they wanted to spend their time elsewhere. I applaud them for that decision. I'd rather have LINQ or async than this obscure feature.

like image 32
usr Avatar answered Oct 22 '22 05:10

usr


Because, in order to get the expression tree, we need a representation of the method in (uncompiled) source form. Lambda expressions are locally available in the source code and therefore are always available uncompiled. But methods may not be from inside the current assembly, and may thus be available only in compiled form.

Granted, the C# compiler could decompile the assembly’s IL code to retrieve an expression tree but as you mentioned, implementing feature costs money, this particular feature isn’t trivial, and the benefits are unclear.

like image 160
Konrad Rudolph Avatar answered Oct 22 '22 06:10

Konrad Rudolph