究竟是谁最后决定什么是泛型类型? [英] Who actually last decide what is the Generic Type?

查看:132
本文介绍了究竟是谁最后决定什么是泛型类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能

 public static T2 MyFunc<T1, T2>( T1 a, T1 b, T2 c)
        {
            return c;
        }     

我要创建2人的类实例:

I'm creating 2 Persons class instances:

 class Person
         {  }

            Person p = new Person();
            Person p2 = new Person();

我打电话的功能:

I'm calling the function with :

 MyClass.MyFunc(p, p2, 5);

我的问题是:

my Question is :

究竟是谁决定对T1类型? (P?P2?)

Who actually decide about the T1 type ? (p ? p2 ? )

因为如果左边是苹果让他检查了第二个也是苹果

Because if the left one is Apple so he checks that the second one is Also an apple

如果第二个是橙色 - 。他应该检查第一个也是橙

and if the second one is Orange - he should check that the first one is also an Orange.

这似乎不可思议问它监守在编译时,他们会失败,如果不一样的。

It seems weird to ask it becuase at compile time they will fail if not the same.

不过 - 谁决定对类型

Still - who decide about the type ?

和第二次 - 如果我把它改为动态 - 上runtime-谁将会决定什么是T1类型应该是

And second - If i change it to dynamic - on runtime- who will decide what the T1 type should be ?

推荐答案

在一个较高的水平,方法类型推断是这样工作的。

At a high level, method type inference works like this.

首先,我们做了一个列表中的所有的参数的 - 前pressions您提供 - 及其相应的形参类型的。

First we make a list of all the arguments -- the expressions you supply -- and their corresponding formal parameter type.

让我们来看一个更有趣的例子,比你给的。假设我们有

Let's look at a more interesting example than the one you give. Suppose we have

class Person {}
class Employee : Person {}
...
Person p = whatever;
Employee p2 = whatever;

和相同的呼叫。所以我们做出了对应:

and the same call. So we make the correspondences:

p  --> T1
p2 --> T1
5  --> T2

然后我们做了什么界限是每个类型参数列表以及它们是否固定。我们有两个类型参数,我们开始没有上限,下限或确切的边界。

Then we make a list of what "bounds" are on each type parameter and whether they are "fixed". We have two type parameters, and we start with no upper, lower or exact bounds.

T1: (unfixed) upper { }  lower { }  exact { }
T2: (unfixed) upper { }  lower { }  exact { }

(回忆在大约类型的相对尺寸的另一个问题我们的最近的讨论是基于一种类型是否是或多或少限制性的;一个类型,是更严格的是的较小的比一个是。限制较少长颈鹿比动物小,因为更多的事情是动物比是长颈鹿的上和下的约束集是完全的是:解决类型推理问题对于给定类型参数必须的大于或等同于的每一个下界和小于或等同于的每一个上限,而的每一个具体的约束。)

(Recall our recent discussion in another question about the relative sizes of types being based on whether or not a type was more or less restrictive; a type that is more restrictive is smaller than one that is less restrictive. Giraffe is smaller than Animal because more things are Animals than are Giraffes. The "upper" and "lower" bound sets are exactly that: the solution to the type inference problem for a given type parameter must be larger than or identical to every lower bound and smaller than or identical to every upper bound, and identical to every exact bound.)

然后我们看看每个参数及其相应的类型。 (如果参数lambda表达式那么我们会弄清楚的的中,我们看一下参数,但是你没有任何lambda表达式在这里让我们忽略的细节。)对于我们做每一个参数一个推断的为正式参数类型,并补充说,我们推断出关于推理的绑定集的事实。所以在看的第一个参数,我们推​​导出界:

Then we look at each argument and its corresponding type. (If the arguments are lambdas then we might have to figure out the order in which we look at arguments, but you don't have any lambdas here so let's ignore that detail.) For each argument we make an inference to the formal parameter type, and add the facts that we deduce about that inference to the bound set. So after looking at the first argument, we deduce the bounds:

T1: (unfixed) upper { }  lower { Person }  exact { }
T2: (unfixed) upper { }  lower { }  exact { }

在第二个参数,我们推​​断出界

After the second argument we deduce the bounds

T1: (unfixed) upper { }  lower { Person, Employee }  exact { }
T2: (unfixed) upper { }  lower { }  exact { }

第三个参数后,我们推导出界:

After the third argument we deduce the bounds:

T1: (unfixed) upper { }  lower { Person, Employee }  exact { }
T2: (unfixed) upper { }  lower { int }  exact { }

在我们,因为我们可以,我们修正作出很大进展的范围通过寻找边界的的最佳类型设置满足每一个绑定的。

After we have made as much progress as we can, we "fix" the bounds by finding the best type in the bounds set that satisfies every bound.

有关T1,有两种类型的边界设定,员工。是否有其中之一即满足每一个绑定在边界集?是。 员工不满足的约束,因为员工是一个型比小; 下限的 - 它意味着的人员没有类型小是合法的的。 确实满足所有的界限:<$​​ C $ C>人与和比员工大,所以它满足这两个边界。在边界的最佳类型设置满足每一个绑定的是T1的和T2显然是 INT ,因为只有一种类型在时间为T2设定的界限。因此,我们再解决这个类型参数:

For T1, there are two types in the bounds set, Person and Employee. Is there one of them that satisfies every bound in the bounds set? Yes. Employee does not satisfy the Person bound because Employee is a smaller type than Person; Person is a lower bound -- it means no type smaller than Person is legal. Person does satisfy all the bounds: Person is identical to Person and is larger than Employee, so it satisfies both bounds. The best type in the bounds set that satisfies every bound is for T1 is Person and for T2 obviously it is int because there is only one type in the bounds set for T2. So we then fix the type parameters:

T1: (fixed) Person
T2: (fixed) int

然后我们问:难道我们有一个固定的区域为所有类型的参数?答案是是,那么类型推断成功。

Then we ask "do we have a fixed bound for every type parameter?" and the answer is "yes", so type inference succeeds.

如果我改变了第一个参数的类型为动态再怎么T1推断?

If I change the first argument's type to dynamic then how is T1 inferred?

如果任何参数是动态的,那么T1和T2推断推迟到运行时,此时语义分析认为的最派生访问运行时类型的值类型的为下限提供由动态参数。

If any argument is dynamic then inference of T1 and T2 is deferred until runtime, at which point the semantic analyzer considers the most derived accessible runtime type of the value as the type for the lower bound supplied by the dynamic argument.

如果你和你想了解更多这个问题的兴趣,还有我的视频在这里解释的C#3.0版本的算法:

If this subject interest you and you want to learn more, there is a video of me explaining the C# 3 version of the algorithm here:

<一个href="http://blogs.msdn.com/b/ericlippert/archive/2006/11/17/a-face-made-for-email-part-three.aspx">http://blogs.msdn.com/b/ericlippert/archive/2006/11/17/a-face-made-for-email-part-three.aspx

(C#3没有上界,只有更低和详细的界限;除此之外,该算法是pretty的大致相同。)

(C# 3 did not have upper bounds, only lower and exact bounds; other than that, the algorithms are pretty much the same.)

一些文章,我写了类型推断的问题在这里:

A number of articles I've written about type inference problems are here:

<一个href="http://blogs.msdn.com/b/ericlippert/archive/tags/type+inference/">http://blogs.msdn.com/b/ericlippert/archive/tags/type+inference/

这篇关于究竟是谁最后决定什么是泛型类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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