【问题标题】:Type inference with multiple generic type parameters具有多个泛型类型参数的类型推断
【发布时间】:2013-03-04 21:43:43
【问题描述】:

我不明白为什么 C# 在以下完整情况下不推断类型:

public interface IThing {}

public class Thing1 : IThing {}

public class Thing2 : IThing {}

public interface IContainer {}

public class Container1 : IContainer
{
    public IThing A { get { return new Thing1(); } }
    public IThing B { get { return new Thing2(); } }
}

public class Container2 : IContainer
{
    public IThing C { get { return new Thing1(); } }
    public IThing D { get { return new Thing2(); } }
}

public class SomeClass
{
    public void PerformTask() {}
}

public static class ExtensionMethods
{
    // This function behaves as I would expect, inferring TContainer
    public static TContainer DoStuffWithThings<TContainer>(this TContainer container, Func<TContainer, IThing> getSomething, Func<TContainer, IThing> getSomethingElse) 
        where TContainer : IContainer
    {
        var something = getSomething.Invoke(container);
        var somethingElse = getSomethingElse.Invoke(container);

        // something and something else are the things we specify in our lambda expressions with respect to the container

        return container;
    }

    // The method in question
    public static TCustomReturnType DoStuffWithThings<TContainer, TCustomReturnType>(this TContainer container, Func<TContainer, IThing> getSomething, Func<TContainer, IThing> getSomethingElse)
        where TContainer : IContainer
        where TCustomReturnType : new()
    {
        var something = getSomething.Invoke(container);
        var somethingElse = getSomethingElse.Invoke(container);

        // Do stuff with the things just as above

        // This time we return our custom type
        return new TCustomReturnType();
    }
}

public class Driver
{
    public static void Main(string[] args)
    {
        var container1 = new Container1();
        var container2 = new Container2();
        // I can do stuff with the things for each container, returning the container each time.

        container1.DoStuffWithThings(c => c.A, c => c.B)
                  .DoStuffWithThings(c => c.B, c => c.A);

        container2.DoStuffWithThings(c => c.C, c => c.D)
                  .DoStuffWithThings(c => c.D, c => c.C);

        // Now we try to do the same but with the custom return type
        container1.DoStuffWithThings<Container1, SomeClass>(c => c.A, c => c.B)
            .PerformTask();
        // As you can see, the compiler requires us to specify Container1 as the container type.
        // Why is it not inferred? It is called from an instance of Container1.
        // The behavior I expect is for container1.DoStuffWithThings<SomeClass>(...) to infer
        // the container type and return a new instance of SomeClass.
    }
}

基本思想是,当只有一个泛型类型参数时,编译器会推断类型。当我添加第二个时,编译器也不会推断(它显然无法推断出第二个,但我不确定为什么它不能推断出第一个)。我的问题是为什么没有推断出容器的类型?

【问题讨论】:

    标签: c# generics extension-methods


    【解决方案1】:

    原因是第二个类型参数它没有在任何函数参数中使用。所以不可能单纯从参数的使用情况来推断它的类型。

    如果你有这样的方法签名(显然不等同于你的代码,只是一个例子):

    public static TResult DoStuffWithThings<TContainer, TResult>(
        this TContainer container,
        Func<TContainer, TResult> getSomething)
    

    然后它将能够从参数中推断出泛型类型。

    如果您想避免指定第一个参数,您可以将方法拆分为两个函数并在泛型类型中隐藏中间参数,如下所示:

    public static IntermediateResult<T> DoStuffWithThings<T>(this T container)
    
    public class IntermediateResult<T>
    {
        public WithReturnType<TResult>()
    }
    

    然后调用为

    var result = container.DoStuffWithThings().WithReturnType<Result>();
    

    【讨论】:

    • 我不希望编译器推断出第二个参数(正如你所说,它没有用于任何参数)。但我确实希望它能够推断出第一个参数(在参数中)。所以我可以调用 container1.DoStuffWithThings(...) 并让它推断 TContainer 是 Container1。在进行类型推断时,参数是全有还是全无?
    • 您必须指定 all 类型参数或 none,但请参阅我更新的答案以获取替代方法。
    • 谢谢。我没有意识到这是全有或全无。对于后代,我很想知道为什么;仅推断已知类型并指定未知类型的设计会在哪里崩溃?
    • @perrick 还认为很难区分Foo&lt;T&gt;(...)(一种类型参数,显式声明)和Foo&lt;T, TInferred&gt;(两种类型参数,只有一种显式声明)。跨度>
    • 如果语言允许您使用Foo&lt;,T&gt;(...) 来澄清您指定的第二个类型参数,同时允许推断第一个类型参数,那就太好了。 Foo(...) 在这种情况下只是Foo&lt;,&gt;(...) 的快捷方式。
    猜你喜欢
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多