C#泛型不能推断出第二个参数? [英] C# generics can't infer second parameter?

查看:127
本文介绍了C#泛型不能推断出第二个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,C#编译器不能推断出第二个泛型参数。
例如:

I've noticed that the C# compiler doesn't infer second generic parameter.
Example:

C ++模板code:(是啊,我知道,模板不工作像泛型)

C++ template code: (yea I know that templates don't work like generics)

class Test {
public:
template <class T,class V> 
    T test(V v) {
       //do something with v
       return T();
    }
};

int i = 0;
Test t = new Test();
double j = t.test<double>(i); //infers V as int

模板(和泛型)无法推断返回类型,所以在C ++我给它的第一个模板参数,而第二个模板参数从变量类型推断出来的。

The templates (and generics) can't infer return type, so in C++ I give it the first template parameter, and the second template parameter is inferred from the variable type.

现在,同样的例子在C#:<​​/ P>

Now, same example in C#:

class Test {
    public T test<T,V>(V v) where T: new() {
       //do something with v
       return new T();
    }
};

int i = 0;
Test t = new Test();
double j = t.test<double>(i); //Error Using the generic method 'Test.test<T,V>(V)' requires '2' type arguments

但是,如果我用1型,我没有显式地指定类型:

But if i use 1 type, I don't have to explicitly specify the type:

class Test {
    public V test<V>(V v) where V: new() {
       return new V();
    }
};

int i = 0;
Test t = new Test();
int j = t.test(i); //OK infers V as int.

因此​​,为什么无法C#泛型推断第二类(而在C ++模板清楚即可)?
我敢肯定它是这样设计的(我怀疑他们的净球队忽略这一点),所以为什么是这样设计的,我必须明确指定这两种类型的?

So, why can't C# generics infer the second type (while in c++ templates it clearly can) ?
I'm sure it's designed that way (I doubt they the .Net team overlooked this), so why is it designed this way that I must explicitly specify both types?

编辑:

这是我们在答案到目前为止,无论是语言支持的模板参数数量超载,讨论

From the discussions we had in the answers so far, both languages support overloading by number of template parameters.

如此反复,为什么是C#设计的这种方式?是什么在语言实现,不允许显式声明只有一个参数有什么不同?

So again, why is C# designed this way ? What's different in the language implementation that doesn't allow to explicitly declare only one parameter ?

推荐答案

C#已经被设计为一个稍微不那么脑弯曲的语言比C ++。

C# has been designed to be a slightly less brain-bending language than C++.

在特别的,我不认为这是一个伟大的想法,比较C#泛型与C ++模板由于各种原因 - 他们根本2真的完全不同的方法来完成的类似于的东西的有些的情况。 C ++的做法肯定是灵活的,在某些方面 - 虽然它不允许(据我所知),将在运行时创建的,只存在于二进制形式,或者新的模板特化的模板。基本上,C ++模板方法没有用.NET如何适合一起休息坐好。

In particular, I don't think it's a great idea to compare C# generics to C++ templates for various reasons - they're fundamentally two really quite different approaches to accomplishing similar things in some situations. The C++ approach is certainly flexible in some ways - although it doesn't allow (as I understand it) templates which only exist in binary form, or new template specializations to be created at execution time. Basically the C++ templating approach doesn't sit well with the rest of how .NET fits together.

现在至于为什么你不能指定的部分的类型参数并允许其他人推断(这是一个的语言的决定,而不是一个的平台的决定;我敢肯定,这将是可行的,只要.NET本身而言) - 再次,我相信这是为了简单起见。选择确切的正确的方法的的正确类型的参数已经在C#中极其复杂 - 更复杂的比大多数C#开发人员可以得到他们的头轮。它包括:

Now as for why you can't specify some type arguments and allow others to be inferred (which is a language decision rather than a platform decision; I'm sure it would be feasible as far as .NET itself is concerned) - again, I believe this is for the sake of simplicity. Choosing the exact right method and the right type arguments is already extremely complicated in C# - more complicated than most C# developers can get their heads round. It involves:

  • 在潜在的考虑方法,从目标的编译时类型的类型层次结构
  • 将参数个数重载
  • 将数量超载的键入的参数
  • 命名参数的影响
  • 可选参数的影响
  • 在对参数类型泛型类型参数约束的影响(没有的的目标方法,注意事项规定的约束)
  • 方法组委派的转换
  • 在匿名函数转换
  • 类型推断类型参数
  • 动态类型
  • 通用​​协变和逆变
  • Potentially considering methods up the type hierarchy from the compile-time type of the target
  • Overloading by number of parameters
  • Overloading by the number of type parameters
  • The effect of named arguments
  • The effect of optional parameters
  • The effect of generic type parameter constraints on parameter types (not constraints specified by the target method, note)
  • Method group to delegate conversions
  • Anonymous function conversions
  • Type inference for type arguments
  • Dynamic typing
  • Generic covariance and contravariance

我个人认为这是足以让我的头左右,而不允许的但更多的的通过M possiblities仍然是候选人是否有至少的许多类型参数指定类型的参数。你也想​​命名的类型参数和可选的类型参数? ;)

Personally, I think that's enough to get my head around, without allowing yet more possiblities via "M can still be a candidate if it has at least as many type parameters as specified type arguments". Would you also want named type arguments and optional type parameters? ;)

我看着超载了不少,完全按照规范等我发现它使语言设计者摸不着头脑,并尝试找出什么样的编译器应该做的领域。我发现,编译器肯定得到错误的领域。 我也不想在这里添加更多的复杂性没有一个很好的理由。

I've looked at overloading quite a lot, following the spec thoroughly etc. I've found areas which make the language designers scratch their heads and try to work out what the compiler should do. I've found areas which the compiler definitely gets wrong. I wouldn't want to add any more complexity here without a really good reason.

所以,是的,它基本上为简单起见,有时候这是一个痛苦 - 但往往你可以解决它。对于每一个潜在的功能,你需要考虑:

So yes, it's basically for the sake of simplicity, and sometimes that's a pain - but often you can work around it. For every potential feature, you need to consider:

  • 的功能的好处结束开发商
  • 要素的成本,最终开发商在时间花在理解它
  • 在设计和全面指定它的成本,语言设计者
  • 在正确实现它的成本,编译器编写者
  • 在彻底测试它的成本,测试团队(一切结合其他各地超载)
  • 的成本,未来发展潜力的功能(如果这个人让语言更加复杂,留下少可能grokable附加其它功能的复杂性)

这篇关于C#泛型不能推断出第二个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆