S,T>&Func键LT的笔;从lambda表达式的输出推断,只有当S和T有什么不同? [英] T of Func<S, T> is inferred from output of lambda expression only when S and T are different?

查看:173
本文介绍了S,T>&Func键LT的笔;从lambda表达式的输出推断,只有当S和T有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

取值 T 是不同的,这个作品:

When S and T are different, this works:

public static void Fun<S, T>(Func<S, T> func)
{

}

Fun((string s) => true); //compiles, T is inferred from return type.



不过,

But,

public static void Fun<T>(Func<T, T> func)
{

}

Fun(t => true); //can't infer type.

在第一个例子,因为 T 是从lambda表达式的返回类型推断,不能 T 在第二个例子中也可以推断?我想它这样做,但为什么第一个 T 不知道,当第二个 T FUNC< T,T> 是已知的,毕竟 T ==牛逼吧?或者是有类型的 Func键的 S'

In the first example, since T is inferred from return type of lambda expression, can't T in the second example too be inferred? I suppose its doing it, but why is first T not known, when second T of Func<T, T> is known, after all T == T right? Or is there an order for types being inferred in case of Funcs?

推荐答案

它没有任何关系与S和T是不同的。它与你在第一种情况下提供正式的参数类型,而不是在第二种情况下提供它做的。

It has nothing whatsoever to do with S and T being different. It has to do with you supplying the formal parameter type in the first case and not supplying it in the second case.

方法的类型推断不会试图推断返回类型从一个lambda委托的的,直至委托的形式参数类型称为的。

Method type inference does not attempt to infer the return type of a delegate from a lambda until the formal parameter types of the delegate are known.

在第二种情况下,你已经给编译器没有与推断正式参数类型T,因此拉姆达的身体甚至不会进行分析。

In the second case you've given the compiler nothing with which to infer the formal parameter type T, and therefore the body of the lambda will not even be analyzed.

你是什么意思正式参数类型?

What do you mean by "formal parameter type"?

一个形式参数是一个变量,需要在传递给方法,索引器,构造函数的lambda或匿名方法的参数的值。 (或者,在的情况下退出 REF 形式参数,成为一个别名调用者提供的变量。 )形式参数的变量的,所以具有的类型

A formal parameter is a variable that takes on the value of an argument passed to a method, indexer, constructor, lambda or anonymous method. (Or, in the case of out and ref formal parameters, becomes an alias to a variable supplied by the caller.) Formal parameters are variables, and therefore have types.

委托委托 - [R FUNC< A,R>(A一); 的形式参数 A 类型 A 。您构建与方法类型参数,使 Func键< S,T> ,所以正式参数类型的委托,现在是取值。类型推断的任务是推断这些类型取值 T

The delegate delegate R Func<A, R>(A a); has formal parameter a with type A. You construct that with method type parameters to make Func<S, T>, so the formal parameter type of the delegate is now S. The task of type inference is to infer those types S and T.

在你的第一个例子中,你有形式参数取值类型字符串的lambda 。所以类型推断的原因是因为这拉姆达参数对应的形式参数 FUNC 方法玩转,并正式参数类型 FUNC Func键< S,T> 然后正式参数类型的取值必须对应于取值。既然你给了一个正式的参数类型为取值取值被推断为字符串

In your first example you have a lambda with formal parameter s of type string. So type inference reasons that since this lambda argument corresponds to formal parameter func of method Fun, and the formal parameter type of func is Func<S, T> then the formal parameter type of s must correspond to S. Since you gave a formal parameter type for s, S is inferred to be string.

一旦推断,就这样 T 可以通过分析拉姆达的身体来推断。

Once that inference is made then T can be inferred by analyzing the body of the lambda.

在你的第二个案例,有一个 T 给没有正式的参数类型。由于没有别的从 T 的类型可以推断,类型推断放弃,放弃看着身前分析这个拉姆达。

In your second case, there is no formal parameter type given for t. Since there is nothing else from which the type of t can be deduced, type inference gives up and abandons analyzing this lambda before looking at the body.

这只是恰巧在你的情况下,身体可以分析独立正式参数类型拉姆达的。这是一个罕见的情况和类型推理算法不写入利用它。

It just so happens that in your case the body could be analyzed independently of the formal parameter type of the lambda. That is a rare case and the type inference algorithm was not written to take advantage of it.

如果这是你想要的那种类型推断,可以考虑使用F#代替C#。它有一个更先进的类型推断算法的基础上,辛德雷 - 米尔纳算法。

If this is the sort of type inference you want, consider using F# instead of C#. It has a far more advanced type inference algorithm, based on the Hindley-Milner algorithm.

这篇关于S,T&GT;&Func键LT的笔;从lambda表达式的输出推断,只有当S和T有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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