为什么不能匿名方法被分配到无功? [英] Why can't an anonymous method be assigned to var?

查看:122
本文介绍了为什么不能匿名方法被分配到无功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

Func<string, bool> comparer = delegate(string value) {
    return value != "0";
};

不过,以下不会编译:

However, the following does not compile:

var comparer = delegate(string value) {
    return value != "0";
};

为什么不能编译器弄清楚它是一个 Func键&LT;字符串,布尔&GT; ?它需要一个字符串参数,并返回一个布尔值。相反,它给我的错误:

Why can't the compiler figure out it is a Func<string, bool>? It takes one string parameter, and returns a boolean. Instead, it gives me the error:

不能到指定匿名方法   隐式类型的局部变量。

Cannot assign anonymous method to an implicitly-typed local variable.

我有一个猜想,这是的如果变种版本编译的,它会缺乏一致性,如果我有以下几点:

I have one guess and that is if the var version compiled, it would lack consistency if I had the following:

var comparer = delegate(string arg1, string arg2, string arg3, string arg4, string arg5) {
    return false;
};

以上就不会因为Func键与其中任何意义;>只允许最多4个参数(在.NET 3.5,这是我现在用的)。也许有人可以澄清这个问题。谢谢你。

The above wouldn't make sense since Func<> allows only up to 4 arguments (in .NET 3.5, which is what I am using). Perhaps someone could clarify the problem. Thanks.

推荐答案

其他人已经指出,有您的无限多的可能的委托类型可以的意思了;有什么特别之处 Func键,不愧是默认的,而不是 predicate 动作或任何其他可能性?而且,对于lambda表达式,这样做的目的是选择委托的形式,而不是前pression树的形式,为什么是明摆着的吗?

Others have already pointed out that there are infinitely many possible delegate types that you could have meant; what is so special about Func that it deserves to be the default instead of Predicate or Action or any other possibility? And, for lambdas, why is it obvious that the intention is to choose the delegate form, rather than the expression tree form?

但我们可以说, Func键是特殊的,而推断出的类型的lambda或匿名方法是什么Func键。我们还是有各种问题。你想被推断为有下列情形是什么类型的?

But we could say that Func is special, and that the inferred type of a lambda or anonymous method is Func of something. We'd still have all kinds of problems. What types would you like to be inferred for the following cases?

var x1 = (ref int y)=>123;

没有 Func键&LT; T&GT; 键入需要参考什么

var x2 = y=>123;

我们不知道正式参数的类型,但我们知道的回报。 (还是我们?是返回INT?长?短?字节?)

We don't know the type of the formal parameter, though we do know the return. (Or do we? Is the return int? long? short? byte?)

var x3 = (int y)=>null;

我们不知道返回类型,但不能是无效的。返回类型可以是任何引用类型或可空值类型。

We don't know the return type, but it can't be void. The return type could be any reference type or any nullable value type.

var x4 = (int y)=>{ throw new Exception(); }

此外,我们不知道的返回类型,这一次,它的可以的是无效的。

var x5 = (int y)=> q += y;

是打算成为一个空隙返回语句的lambda或东西,返回被赋予Q值?两者都是合法的;而我们应该选择?

Is that intended to be a void-returning statement lambda or something that returns the value that was assigned to q? Both are legal; which should we choose?

现在,你可能会说,好了,就是不支持任何这些功能。只是支持那里的类型都可以制定出正常的情况下。这并没有帮助。如何使我的生活更容易吗?如果该功能的工作原理有时并未能有时候然后我还得写code到的检测的所有这些失败的情况下,给予的有意义的错误信息的每一个。我们仍必须指定所有的行为,记录它,为它编写测试,等等。这是一个非常昂贵的特点,节省了用户也许半打按键。我们有更好的方法来增加价值,语言不是花费大量的时间编写测试用例的,这并不一半的工作时间,不提供几乎没有任何益处在它的工作情况的功能。

Now, you might say, well, just don't support any of those features. Just support "normal" cases where the types can be worked out. That doesn't help. How does that make my life easier? If the feature works sometimes and fails sometimes then I still have to write the code to detect all of those failure situations and give a meaningful error message for each. We still have to specify all that behaviour, document it, write tests for it, and so on. This is a very expensive feature that saves the user maybe half a dozen keystrokes. We have better ways to add value to the language than spending a lot of time writing test cases for a feature that doesn't work half the time and doesn't provide hardly any benefit in cases where it does work.

它实际上是有用的情况是:

The situation where it is actually useful is:

var xAnon = (int y)=>new { Y = y };

由于没有朗读类型的东西。但是,我们有这个问题的时候,我们只是使用的方法类型推断来推导类型:

because there is no "speakable" type for that thing. But we have this problem all the time, and we just use method type inference to deduce the type:

Func<A, R> WorkItOut<A, R>(Func<A, R> f) { return f; }
...
var xAnon = WorkItOut((int y)=>new { Y = y });

和现在的方法类型推断的工作出了什么FUNC类型。

and now method type inference works out what the func type is.

这篇关于为什么不能匿名方法被分配到无功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆