我搜索了一些关于类型推断的内容,但我似乎无法将任何解决方案应用于我的特定问题。
我在构建和传递函数方面做了很多工作。在我看来,这应该能够推断出 int 类型。我唯一能想到的是类型推断算法不检查 lambda 返回类型。我删除了不必要的逻辑以更清楚地显示问题。
Func<T> Test<T>(Func<Func<T>> func)
{
return func();
}
这个编译:
Func<int> x = Test<int>(() =>
{
int i = 0;
return () => i;
});
但这会给出错误“无法从用法中推断出方法的类型参数。尝试明确指定类型参数”:
Func<int> x = Test(() =>
{
int i = 0;
return () => i;
});
我想我只是想知道为什么它以这种方式工作以及任何解决方法。
最佳答案
我想说这个问题的正确答案是由 E.Lippert 在 SO Why can't an anonymous method be assigned to var? 中给出的
但是让我们稍微玩一下你的例子:
Func<Func<int>> f = () =>
{
int i = 0;
return () => i;
};
Func<int> x = Test(f); //it compiles OK
使用您的 Func<T> Test<T>(Func<Func<T>> func)
进行类型推断没有问题这里。
问题隐藏在您使用匿名 lambda 表达式,无法推断其类型。试试这个:
var f = () =>
{
int i = 0;
return () => i;
};
它给出 Compiler Error CS0815 , 说
Cannot assign lambda expression to an implicitly-typed local variable
解释是:
An expression that is used as the initializer for an implicitly typed variable must have a type. Because anonymous function expressions, method group expressions, and the null literal expression do not have a type, they are not appropriate initializers. An implicitly typed variable cannot be initialized with a null value in its declaration, although it can later be assigned a value of null.
现在让我们尝试另一件事:
var x = Test(() =>
{
Func<int> f = () => 0;
return f;
});
它也可以编译。所以你原来的例子的问题实际上是这一行:
return () => i;
我们可以更进一步,根据 Eric Lippert 在他的回答中所说的,提供另一个函数来包装它:
static Func<T> GetFunc<T>(Func<T> f) { return f; }
现在我们可以像这样重写您的代码:
var x = Test(() =>
{
int i = 0;
return GetFunc(() => i);
});
而且效果也很好。
但是,据我所知,这都是开销,您应该只提供一个显式类型。虽然这些变通办法是合适的,但当您需要一个 lambda 时,返回一个匿名类型的对象。
关于c# - 嵌套泛型函数的类型推断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15696520/