简体   繁体   English

类型参数的C#variance注释,约束为值类型

[英]C# variance annotation of a type parameter, constrained to be value type

It is possible in C# to add variance annotation to type parameter, constrained to be value type: 在C#中可以将方差注释添加到类型参数,约束为值类型:

interface IFoo<in T> where T : struct
{
  void Boo(T x);
}

Why is this allowed by compiler if variance annotation makes completely no sense in a such situation? 如果在这种情况下方差注释完全没有意义,为什么编译器允许这样做?

Why this is allowed by compiler since variance annotation make completely no sense in a such situation? 为什么编译器允许这样做,因为在这种情况下,方差注释完全没有意义?

It's allowed by the compiler because I never even considered that someone might try to do that when I added the variance rules to the C# 4.0 compiler. 它是编译器允许的,因为当我将方差规则添加到C#4.0编译器时,我甚至从未考虑过有人会尝试这样做。

Compiler warnings and errors are features , and in order for a feature to be implemented, it has to, at a bare minimum, be thought of at some point before you ship your compiler . 编译器警告和错误是功能 ,为了实现功能,在发布编译器之前 ,必须至少考虑它 I failed to do so, and therefore never had the opportunity to even debate whether there ought to be a warning for such a situation. 我没有这样做,因此从来没有机会甚至辩论是否应该对这种情况发出警告。

Now that you've brought it to my attention, the question is: should it be a feature? 现在您已经引起了我的注意,问题是:它应该是一个功能吗? Should the compiler produce a warning (or error) for this case? 编译器是否应该针对此案例发出警告(或错误)?

That's a judgment call. 这是一个判断电话。 A number of things we'd consider are: 我们考虑的一些事情是:

  • Is the code the sort of thing someone might type in thinking it does something sensible? 代码是某种人可能会认为它做了一些合理的事情吗? One hopes not; 人们不希望; one hopes that the developer who knows enough about the type system to make an interface variant also knows that variance only works on reference types. 人们希望那些了解类型系统的开发人员能够创建一个接口变体也知道方差仅适用于引用类型。 But maybe there are developers out there who might type this in thinking that it will work. 但也许有些开发人员可能会认为它会起作用。 It doesn't seem beyond plausibility at least. 至少它似乎没有合理性。 It's not clearly contrived. 它没有明显的设计。

  • Is the code clearly wrong ? 代码明显错了吗? Yes, it probably is. 是的,它可能是。 It seems very unlikely that someone deliberately wants to write an interface that looks variant but in fact is not. 似乎不太可能有人故意想要编写一个看似变体但实际上并非如此的界面。

And so on. 等等。

I'll have to give it more thought, but at first glance it looks like this actually might be a decent warning to add to the compiler. 我将不得不更多地考虑它,但乍一看看起来这实际上可能是添加到编译器的一个不错的警告。 I'll talk it over with the team and we'll consider adding it to the Roslyn version. 我将与团队讨论,我们会考虑将其添加到Roslyn版本中。

Thanks for the idea! 谢谢你的想法!

It is allowed simply because it is legal code. 它只是因为它是合法代码而被允许。 There is absolutely no harm in it. 它完全没有害处。 Yes, you can not use contravariant conversion, but I fail to see the problem. 是的,你不能使用逆变换,但我没有看到问题。 Nothing in the code will actually be misleading or hide some twisted gotcha . 代码中的任何内容都不会产生误导或隐藏一些扭曲的问题

I simply think the compiler doesn't check if T is a value type or a reference type when checking for variance validity. 我只是认为编译器在检查方差有效性时不检查T值类型还是引用类型 It stands to reason that the C# team assumed that anyone using generic interface variance would know that doing so with value types is pointless and in in any case has no secondary effects. 按理说,C#团队认为使用通用接口方差的任何人都会知道使用值类型这样做是没有意义的,并且在任何情况下都没有副作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM