[英]Upper-Bound/Lower-Bound Inferences and fixing
In the C# 4.0 Spec 7.5.2.9: 在C#4.0规范7.5.2.9中:
A lower-bound inference from a type U to a type V is made as follows:
从类型U到类型V 的下限推断如下:
I've gone over this section many times. 我已多次浏览过这一部分。 Lacking a section reference, this definition reads like a circular reference.
缺少一个部分引用,这个定义读起来就像一个循环引用。 So, I expect to find a grammer production or section reference nearby to clarify..which I do not.
所以,我希望在附近找到一个语法制作或章节参考来澄清......我不知道。 This section also ties in Fixing which suffers from similar definition issues.
本节还涉及Fixing ,它遇到类似的定义问题。
What is an upper-bound inference
vs a lower-bound inference
? 什么是
upper-bound inference
与lower-bound inference
?
I'll try my best to describe it more clearly. 我会尽力把它描述得更清楚。 Worst case, I describe it differently .
最糟糕的情况是,我的描述不同 。
The upper/lower inference is one part of a phased approach to type inference with regard to type arguments that are used for a particular generic method call. 上/下推断是关于用于特定泛型方法调用的类型参数的类型推断的分阶段方法的一部分。 Obviously, upper/lower inference won't be applied if in the first phase if the argument (E) is explicitly typed.
显然,如果在第一阶段中明确键入参数(E),则不会应用上/下推断。 eg:
例如:
given 特定
public static T Choose<T>(T first, T second) {
return (rand.Next(2) == 0)? first: second;
}
I can invoke Choose
with explicit type arguments: 我可以使用显式类型参数调用
Choose
:
Choose<String>("first", "second");
With regard to the upper- or lower-bounds inference, there are some implications throughout 7.5.2 that decide whether lower- or upper-bounds inference is even applicable. 关于上限或下限推断,7.5.2中的一些含义决定了下限或上限推断是否适用。 For example, 7.5.2.9 (and .10) detail that the type parameter is unfixed for either upper- or lower-bounds inference to occur.
例如,7.5.2.9(和0.10)详情类型参数是未定影以发生任一大写或较低范围的推断。 7.5.2.5 details that a type parameter is only unfixed when that type parameter depends on another unfixed type parameter.
7.5.2.5详细说明当该类型参数依赖于另一个未固定的类型参数时,类型参数仅不固定。 For example
例如
IEnumerable<TResult> Select<TSource, TResult>(IEnumerable<TSource> e,
Func<TSource, Result> f)
TResult
"depends on" TSource
, because the type of TSource
could possibly determine the type of TResult
. TResult
“依赖” TSource
,因为类型TSource
可能可能确定的类型TResult
。 eg with a call like Select(c, e->Name)
, TResult
depends on the type of Name
in TSource
. 例如,使用
Select(c, e->Name)
之类的调用, TResult
取决于TSource
中Name
的类型。
In terms of upper- and lower-bounds inferences, for a given unfixed type parameter (X) whose type (V) is not explicitly declared (see first paragraph), upper or lower bounds of type argument (E) of type U are deduced. 就上限和下限推断而言,对于未明确声明其类型(V)的给定的未固定类型参数(X)(参见第一段),推导出类型U的类型参数(E)的上限或下限。 。 If the type parameter is covariant (has
out
modifier) and one of the types in the lower-bound set is a candidate for the parameter, then a lower-bound inference occurred. 如果类型参数是协变的(具有
out
修饰符)并且下限集中的一个类型是参数的候选者,则发生下限推断。 Conversely, if the type parameter is contravariant (has 'in' modifier) and one of the types in the upper-bound set is a candidate for the parameter, then an upper-bound inference occurred. 相反,如果类型参数是逆变的(具有'in'修饰符)并且上限集中的一个类型是参数的候选者,则发生上限推断。 eg with
Select(c, e->e.Name)
and c
was IEnumerable<Mammal>
then the compiler would infer an lower bound of Mammal
because the type parameter in IEnumerable
is covariant (eg it's declared IEnumerable<out T>
. If it were declared IEnumerable<in T>
then an upper-bound would be inferred. And if it were declared Enumerabale<T>
--with no in
or out
then it would be invariant and neither upper- nor lower-bounds inference would apply.) 例如,使用
Select(c, e->e.Name)
, c
是IEnumerable<Mammal>
然后编译器将推断出Mammal
的下界 ,因为IEnumerable
的类型参数是协变的(例如,它声明了IEnumerable<out T>
。如果它如果声明IEnumerable<in T>
则会推断出上限。如果它被声明为Enumerabale<T>
- 没有in
或out
那么它将是不变的,并且上边界和下边界推断都不适用。)
Clearly, if parameter type can be neither covariant nor contravariant then an exact match must occur 显然,如果参数类型既不是协变的也不是逆变的,那么必须进行精确匹配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.