10

In the C# 4.0 Spec 7.5.2.9:

A lower-bound inference from a type U to a type V is made as follows:

  • If V is one of the unfixed Xi, then U is added to the set of lower bounds for Xi.
  • [...]

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.

What is an upper-bound inference vs a lower-bound inference?

3
  • 1
    I don't think you can take 7.5.2.9 in isolation. You have to take on the entirety of 7.5.2 to understand what's going on. Feb 18, 2013 at 14:28
  • @Damien_The_Unbeliever - That's kind-a what I'm afraid of. It feels like a badly written program. I have to understand the whole thing just to deal with 1 subsection. I have read through 7.5.2...but there's so much to juggle in my head Xi Xj, U, V, the assumed Generic..... Feb 18, 2013 at 14:31
  • 7.5.2.9 doesn't really define what a lower-bound inference is. It's a set of instructions for what you're meant to do when you find (anywhere in 7.5.2) an instruction of e.g. "a lower-bound inference is made from U to T". And then 7.5.2.11 tells you how these inferences are used. Feb 18, 2013 at 14:35

1 Answer 1

5

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.g.:

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<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. 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.5 details that a type parameter is only unfixed when that type parameter depends on another unfixed type parameter. 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. e.g. with a call like Select(c, e->Name), TResult depends on the type of Name in TSource.

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. 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. 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. e.g. 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 (e.g. 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.)

Clearly, if parameter type can be neither covariant nor contravariant then an exact match must occur

2
  • How the hell do you know this!? Feb 19, 2013 at 23:54
  • Great explanation Peter. Sep 17, 2015 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.