Compiler warning on (ambiguous) method solution with named parameters

One question is whether the following code should give a compiler warning or not (this is not the case). It declares two methods with the same name / return type, which has an additional named / optional parameter with a default value.

NOTE. Technically, authorization is not ambiguous, because the rules clearly state that the first method will be called. See here, Overload Resolution, Third Point Marker . This behavior is also intuitive to me, not a 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 intuitive here. Although the code is technically clean (whether sound design is another question :)).

+6
named-parameters
source share
2 answers

I do not agree that he really needs a warning. The main problem is that this code is potentially legitimate, and if in this case you have to explicitly disable the warning.

What I mean, in general, when you get a warning, you can change your code to get rid of the warning (and presumably make the code better at the same time). But in this case, it is possible that you did it as it was intentionally and could not change your code to get rid of the warning.

For example, the warning “unreachable code” is that you can simply delete the unreachable code to get rid of the warning. Or the warning “could not find the link” is usually a signal that you will get errors like undefined, but if not, you can just delete the link. Or maybe the warning “Previous catch exception has not catch all exceptions”: in this case you need to change your code so that either the new sentence comes before catch-all, or even removes catch.

But the fact is that in each case, when you receive a warning, you must change your code, and making changes will always lead to a "better" code. However, in the case of this question, the call is not ambiguous (as far as the compiler is concerned), and I do not think that you can say that it was always a mistake to write such code, so there should not be a warning.

If the compiler gives a warning about every case when you do something that is probably not the best idea, then we will be awash with warnings!

+1
source share

Warnings should notify programmers of possible dumb errors. This is an area that can cause a foggy error, so yes, it should generate a warning. Are you trying to form a petition?

+1
source share

All Articles