开发者

Calling a method on a lambda expression without requiring a variable declaration

开发者 https://www.devze.com 2023-02-01 04:45 出处:网络
Given this extension method public static void Until(this Action action, Func<bool> booleanFunc)

Given this extension method

    public static void Until(this Action action, Func<bool> booleanFunc)
    {
        while (!booleanFunc())
            action();
    }

is it possible to turn this

        var x = 0;

        Action ac = () => x += 1;
        ac.Until(() => x == 5);

into something of this sort

  开发者_Python百科      var x = 0;

        (() => x += 1).Until(() => x == 5);

That is, a one liner?


Well lambdas need a type. Saying () => x += 1 alone says nothing about the delegate it is supposed to represent. So provide the type of the lambda by passing it into the constructor.

new Action(() => x += 1).Until(() => x == 5);

A cast works as well.

((Action)(() => x += 1)).Until(() => x == 5);


Unfortunately such syntax is not possible because the compiler cannot infer the type of the anonymous expression. It could be Action but it also could be some other custom defined delegate. You will need to cast it to Action.


It's interesting - I discovered the same thing a while ago.

Extension method invocations are described in section 7.6.5.2 of the C# spec, and it seems to me that all of the conditions there are met... because that talks in terms of an expression being implicitly convertible to the first parameter type (which it is, in this case). That suggests that either there's a bug in the compiler (possible, but unlikely) or that one of the earlier conditions for it being a valid method call isn't met.

I suspect the problem is that a lambda expression isn't a primary-expression in spec terminology - unfortunately, my spec-fu isn't up to much this morning due to a cold. A lot of the rest of the method invocation machinery probably relies on knowing the type of the target of the method, which isn't known in this case.

Note that the other conspicuous "untyped expression" has the same problem - you can't call extension methods on a null literal, either.

Very occasionally this limitation is a pain, but most of the time I don't have much of an issue with it - and I suspect it makes the compiler a lot simpler to implement...

0

精彩评论

暂无评论...
验证码 换一张
取 消