.NET: Inferred generic types on static methods

前端 未结 5 1111
失恋的感觉
失恋的感觉 2020-12-29 09:18

Suppose I have

public static List Map(List inputs, Func f)
{
    return inputs.ConvertAll((x) => f(x));
}

pr         


        
相关标签:
5条回答
  • 2020-12-29 09:37

    From your error message:

    The type arguments for method '[...].Map<T,T2>(System.Collections.Generic.List<T>, System.Func<T,T2>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

    Note that the error message says that it can not figure out the type arguments. That is, it is having trouble resolving one of the type parameters T or T2. This is because of §25.6.4 (Inference of type arguments) of the specification. This is the part of the specification the deals with inferring generic type parameters.

    Nothing is inferred from the argument (but type inference succeeds) if any of the following are true:

    [...]

    The argument is a method group.

    Thus, the compiler is not able to use the delegate type of Square to infer the type of T2. Note that if you change the declaration to

    public static List<T> Map<T>(List<T> inputs, Func<T, T> f) {
            return inputs.ConvertAll((x) => f(x));
    }
    

    then

    var outputs = Map(inputs, Square);
    

    is legal. In this case, it has already resolved that T is int from the fact that inputs is a List<int>.

    Now, the deeper question is why is the above the specification? That is, why don't method groups play a role in type parameter resolution? I think it's because of cases like this:

    class Program {
        public static T M<T>(Func<T, T> f) {
            return default(T);
        }
    
        public static int F(int i) {
            return i;
        }
    
        public static float F(float f) {
            return f;
        }
    
        static void Main(string[] args) {
            M(F); // which F am I?
        }
    }
    
    0 讨论(0)
  • 2020-12-29 09:37

    The reason this does not work is for c# to do type inference on a method, it has to know the delegate type at the other end of the conversion. But at this point in time, the target delegate type is still not fully known - only T (int) is known, T2 is still unresolved.

    Func<int, int> f = Square;
    //works because we provided the destination type
    //of the conversion from Square to delegate
    
    Map(inputs, i => Square(i));
    //works because the lambda follows the actual method call
    //and determines its own return type
    
    0 讨论(0)
  • 2020-12-29 09:48

    The inference fails at inferring the type of the delegate, not the list:

    // this is also fine
    var outputs3 = Map(inputs, new Func<int, int>(Square));
    
    // more calls that compile correctly
    var outputs4 = Map(inputs, x => Square(x));
    
    var outputs5 = Map(inputs, x => x * x);
    
    Func<int, int> t = Square;
    var outputs6 = Map(inputs, t);
    

    I don't know why, though - perhaps there's just no implicit typecast from the signature of Square to Func<Int32, Int32>? It seems strange that Func<int, int> t = Square; is valid, but the compiler can't make the leap on its own... Bug, maybe?

    0 讨论(0)
  • 2020-12-29 09:48

    Upon a little digging, I've found your suspicion about the other answer is correct. Here's what the C# 3.0 specification says:

    7.4.2.1 - For each of the method arguments Ei: If Ei is an anonymous function or method group, an explicit parameter type inference (7.4.2.7) is made... 7.4.2.7 - ...If E is an explicitly typed anonymous function with parameter types U1…Uk and T is a delegate type with parameter types V1…Vk then for each Ui an exact inference (§7.4.2.8) is made from Ui for the corresponding Vi.

    In other words, anonymous methods and method groups (which Square is), can only infer the parameter types explicitly. I think the justification at the end of the answer you referred to sums it up nicely. Because a type inference cannot always be made implicitly from a method group, the compiler does not even attempt it, per the specification.

    0 讨论(0)
  • 2020-12-29 10:00

    The following also works; I don't know why:

    var outputs = Map(inputs, i => Square(i));
    
    0 讨论(0)
提交回复
热议问题