为什么不能C#构造推断类型? [英] Why can't the C# constructor infer type?

查看:119
本文介绍了为什么不能C#构造推断类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不支持构造类型推断它是泛型方法的方式吗?

 公共类的MyType< T>
{
   私人只读T字段;
   公众的MyType(T值){字段=值; }
}VAR OBJ =新的MyType(42); //为什么不能推断类型的工作了,我想的MyType< INT&GT ;?

虽然可以解决这个问题有一个工厂类,

 公共类MyTypeFactory
{
   公共静态的MyType< T>创建< T>(T值)
   {
      返回新的MyType< T>(值);
   }
}
变种MyObj中= MyTypeFactory.Create(42);

有没有为什么构造函数不能支持类型推断一个实际或哲学道理?


解决方案

  

有一个哲学之所以构造函数不能支持类型推断?


没有。当你有

 新富(巴)

那么我们就可以找出所有类型的范围称为富而不管通用的元数,然后做使用改进的方法类型推断算法上的每个重载决议。然后,我们不得不创造一个'betterness算法,该算法决定了在具有相同名称但不同属的arity两类2适用的构造的的是更好的构造。为了保持向后相容对非泛型类型构造函数必须总是赢。


  

有没有为什么构造函数不能支持类型推断实际的原因是什么?


是的。即使胜过它的成本要素的利益 - 这是相当大的 - 这还不足以有一个功能来实现。不仅该功能必须是净赢,它必须是一个的的净赢相比,我们只能在被投资的其他可能的功能。它还具有比花费的时间更好和精力bug修复,性能的工作,和其他可能的领域,我们可以把这一努力。理想情况下它具有很好适应到任何的主旋律是释放。

此外,你注意到,就可以得到这个功能的好处,而不真正具有功能本身,使用工厂模式。易变通的存在使得它不太可能是一个功能将永远不会实现。

此功能目前已被可能的功能很长一段时间就行了。这是从来没有真正得到执行就行了近足够高的任何地方。

更新2015年3月

所提出的特征使得它足够接近列表顶部的C#6被指定,并且设计的,但是然后切断。

Why is type inference not supported for constructors the way it is for generic methods?

public class MyType<T>
{
   private readonly T field;
   public MyType(T value) { field = value; }
}

var obj = new MyType(42); // why can't type inference work out that I want a MyType<int>?

Though you could get around this with a factory class,

public class MyTypeFactory
{
   public static MyType<T> Create<T>(T value)
   {
      return new MyType<T>(value);
   }
}
var myObj = MyTypeFactory.Create(42);

Is there a practical or philosophical reason why the constructor can't support type inference?

解决方案

Is there a philosophical reason why the constructor can't support type inference?

No. When you have

new Foo(bar)

then we could identify all types called Foo in scope regardless of generic arity, and then do overload resolution on each using a modified method type inference algorithm. We'd then have to create a 'betterness' algorithm that determines which of two applicable constructors in two types that have the same name but different generic arity is the better constructor. In order to maintain backwards compatibility a ctor on a non-generic type must always win.

Is there a practical reason why the constructor can't support type inference?

Yes. Even if the benefit of the feature outweighs its costs -- which are considerable -- that's not sufficient to have a feature implemented. Not only does the feature have to be a net win, it has to be a large net win compared to all the other possible features we could be investing in. It also has to be better than spending that time and effort on bug fixing, performance work, and other possible areas that we could put that effort. And ideally it has to fit in well to whatever the "theme" is of the release.

Furthermore, as you correctly note, you can get the benefits of this feature without actually having the feature itself, by using a factory pattern. The existence of easy workarounds makes it less likely that a feature will ever be implemented.

This feature has been on the list of possible features for a long time now. It's never been anywhere near high enough on the list to actually get implemented.

UPDATE March 2015

The proposed feature made it close enough to the top of the list for C# 6 to be specified and designed, but was then cut.

这篇关于为什么不能C#构造推断类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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