编译器对具有命名参数的(不明确)方法解析发出警告

发布于 2024-08-29 05:21:22 字数 486 浏览 5 评论 0原文

一个问题是关于以下代码是否应该产生编译器警告(它不会)。它声明了两个具有相同名称/返回类型的方法,其中一个方法有一个带有默认值的附加命名/可选参数。

注意:从技术上讲,该解决方案并不含糊,因为规则明确规定将调用第一个方法。请参阅此处,重载解决方案,第三个要点 。毫无疑问,这种行为对我来说也是直观的。

public void Foo(int arg) { ... }

public void Foo(int arg, bool bar = true) { ...} 

Foo(42); // shouldn't this give a compiler warning?

我认为编译器警告在这里会很直观。尽管代码在技术上是干净的(是否是健全的设计是另一个问题:))。

One question regarding whether the following code should yield a compiler warning or not (it doesn't). It declares two methods of the same name/return type, one has an additional named/optional parameter with default value.

NOTE: technically the resolution isn't ambiguous, because the rules clearly state that the first method will get called. See here, Overload resolution, third bullet point. This behavior is also intuitive to me, no question.

public void Foo(int arg) { ... }

public void Foo(int arg, bool bar = true) { ...} 

Foo(42); // shouldn't this give a compiler warning?

I think a compiler warning would be kind of intuitive here. Though the code technically is clean (whether it is a sound design is a different question:)).

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

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

发布评论

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

评论(2

小红帽 2024-09-05 05:21:22

实际上,我不同意它需要警告。主要问题是,该代码可能是合法的,如果是这种情况,您将必须显式禁用该警告。

我的意思是,一般来说,当您收到警告时,您将能够更改代码以消除警告(并且可能同时使代码变得更好)。但在这种情况下,您可能是故意这样做的,并且无法更改代码来消除警告。

例如,“无法访问的代码”警告是您可以删除无法访问的代码来消除警告。或者“找不到引用”警告 - 这通常是一个信号,表明您将收到“未定义类型”错误,但如果没有,那么您可以简单地删除引用。或者可能是“先前的 catch 子句已捕获所有异常”警告:在这种情况下,您需要更改代码,以便新子句出现在 catch-all 之前,或者完全删除 catch。

但要点是,在每种情况下,当您收到警告时,您应该更改代码,并且进行更改总是会产生“更好”的代码。但是,在这个问题的情况下,调用并不含糊(就编译器而言),我认为您不能认为编写这样的代码总是是一个错误,所以不应该有警告。

如果编译器对您所做的事情可能不是最好的想法的每种情况发出警告,那么我们就会被警告淹没!

I disagree that it needs a warning, actually. The main issue is, that code is potentially legitimate, and if that's the case you would have to explicitly disable the warning.

What I mean is, in general when you get a warning, you will be able to change your code to get rid of the warning (and presumably make the code better at the same time). But in this case, it may be that you did it like that intentionally and would not be able to change your code to get rid of the warning.

For example, the "unreachable code" warning is something you can just delete the unreachable code to get rid of the warning. Or the "could not find reference" warning - this is usually a signal that you're going to get "undefined type" errors, but if not then you can simply delete the reference. Or maybe "A previous catch clause already catches all exceptions" warning: in this case, you need to change your code so that either the new clause comes before the catch-all, or remove the catch altogether.

But the point is that in every case when you get a warning, you should change your code, and making the change will always result in "better" code. However, in the case of this question, the call is not ambiguous (as far as the compiler is concerned) and I don't think you can argue that it's always a mistake to write code like that, so therefore there shouldn't be a warning.

If the compiler issued a warning about every case where you do something that's probably not the best idea, then we'd be inundated with warnings!

爱她像谁 2024-09-05 05:21:22

警告是为了通知程序员潜在的愚蠢错误。这是一个可能会产生愚蠢错误的区域,所以是的,它应该生成警告。您是否正在尝试提出请愿书?

Warnings are to notify programmers of potentially dumb mistakes. This is an area that could generate a dumb mistake, so yes, it should generate a warning. Are you trying to form a petition?

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