c# - Upper-Bound/Lower-Bound Inferences and fixing -
c# - Upper-Bound/Lower-Bound Inferences and fixing -
in c# 4.0 spec 7.5.2.9:
a lower-bound inference type u type v made follows:
if v 1 of unfixed xi, u added set of lower bounds xi. [...]i've gone on section many times. lacking section reference, definition reads circular reference. so, expect find grammer production or section reference nearby clarify..which not. section ties in fixing suffers similar definition issues.
what upper-bound inference
vs lower-bound inference
?
i'll seek best describe more clearly. worst case, describe differently.
the upper/lower inference 1 part of phased approach type inference regard type arguments used particular generic method call. obviously, upper/lower inference won't applied if in first phase if argument (e) explicitly typed. e.g.:
given
public static t choose<t>(t first, t second) { homecoming (rand.next(2) == 0)? first: second; }
i can invoke choose
explicit type arguments:
choose<string>("first", "second");
with regard upper- or lower-bounds inference, there implications throughout 7.5.2 decide whether lower- or upper-bounds inference applicable. example, 7.5.2.9 (and .10) detail type parameter unfixed either upper- or lower-bounds inference occur. 7.5.2.5 details type parameter unfixed when type parameter depends on unfixed type parameter. illustration
ienumerable<tresult> select<tsource, tresult>(ienumerable<tsource> e, func<tsource, result> f)
tresult
"depends on" tsource
, because type of tsource
perchance determine type of tresult
. e.g. phone call select(c, e->name)
, tresult
depends on type of name
in tsource
.
in terms of upper- , lower-bounds inferences, given unfixed type parameter (x) type (v) not explicitly declared (see first paragraph), upper or lower bounds of type argument (e) of type u deduced. if type parameter covariant (has out
modifier) , 1 of types in lower-bound set candidate parameter, lower-bound inference occurred. conversely, if type parameter contravariant (has 'in' modifier) , 1 of types in upper-bound set candidate parameter, upper-bound inference occurred. e.g. select(c, e->e.name)
, c
ienumerable<mammal>
compiler infer lower bound of mammal
because type parameter in ienumerable
covariant (e.g. it's declared ienumerable<out t>
. if declared ienumerable<in t>
upper-bound inferred. , if declared enumerabale<t>
--with no in
or out
invariant , neither upper- nor lower-bounds inference apply.)
clearly, if parameter type can neither covariant nor contravariant exact match must occur
c#
Comments
Post a Comment