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

发布于 2025-01-06 17:48:15 字数 167 浏览 5 评论 0原文

在 C# 中,可以向类型参数添加方差注释,并限制为值类型:

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

如果方差注释在这种情况下完全没有意义,为什么编译器允许这样做?

It is possible in C# to add variance annotation to type parameter, constrained to be value type:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲜肉鲜肉永远不皱 2025-01-13 17:48:15

为什么编译器允许这样做,因为方差注释在这种情况下完全没有意义?

这是编译器允许的,因为当我将方差规则添加到 C# 4.0 编译器时,我什至从未考虑过有人可能会尝试这样做。

编译器警告和错误是功能,为了实现某个功能,必须在发布编译器之前的某个时刻考虑到它 >。我没有这样做,因此从来没有机会辩论是否应该对这种情况发出警告。

现在您已经引起我的注意,问题是:应该它是一个功能吗?编译器是否应该针对这种情况产生警告(或错误)?

这是一个判断。我们要考虑的一些事情是:

  • 代码是否是人们可能会输入的代码,认为它做了一些明智的事情?人们希望不会;人们希望对类型系统有足够了解以制作接口变体的开发人员也知道变体仅适用于引用类型。但也许有些开发人员可能会输入此内容,认为它会起作用。至少这似乎并没有超出合理范围。这不是明显的人为设计。

  • 代码明显错误吗?是的,可能是这样。似乎不太可能有人故意想要编写一个看起来不同但实际上并非如此的界面。

等等。

我必须更多地考虑它,但乍一看,这实际上可能是添加到编译器中的一个不错的警告。我会与团队讨论,我们会考虑将其添加到 Roslyn 版本中。

谢谢你的主意!

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.

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.

Thanks for the idea!

千年*琉璃梦 2025-01-13 17:48:15

它被允许只是因为它是合法的代码。这绝对没有坏处。是的,你不能使用逆变转换,但我没有看到这个问题。代码中的任何内容实际上都不会产生误导或隐藏一些扭曲的问题。

我只是认为编译器在检查方差有效性时不会检查 T值类型还是引用类型。按理说,C# 团队假设任何使用通用接口差异的人都会知道使用值类型这样做是没有意义的,并且在任何情况下都不会产生任何副作用。

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文