void foo(int)
{
}
void main()
{
foo(5);
}
Although anonymous function parameters may be used in function declarations, it seems that dmd doesn't check whether a function declaration is a definition too.
Comment #1 by bearophile_hugs — 2012-09-09T06:53:14Z
(In reply to comment #0)
> void foo(int)
> {
>
> }
>
> void main()
> {
> foo(5);
> }
>
> Although anonymous function parameters may be used in function declarations, it
> seems that dmd doesn't check whether a function declaration is a definition
> too.
What's the problem here?
foo() not giving a name to its first argument means the programmer is stating foo() never uses its first argument, and the D language allows the programmer to enforce this desire.
This feature also allows to avoid "unused argument warnings" once/where such warning is available.
Comment #2 by maxim — 2012-09-09T07:02:06Z
(In reply to comment #1)
> (In reply to comment #0)
> > void foo(int)
> > {
> >
> > }
> >
> > void main()
> > {
> > foo(5);
> > }
> >
> > Although anonymous function parameters may be used in function declarations, it
> > seems that dmd doesn't check whether a function declaration is a definition
> > too.
>
> What's the problem here?
>
> foo() not giving a name to its first argument means the programmer is stating
> foo() never uses its first argument, and the D language allows the programmer
> to enforce this desire.
>
> This feature also allows to avoid "unused argument warnings" once/where such
> warning is available.
The problem is that such code is likely to be a bug (parameter name was forgotten) and compiler should report about this. BTW dmd doesn't produce any warning for unused variable.
Comment #3 by bearophile_hugs — 2012-09-09T13:35:05Z
(In reply to comment #2)
> The problem is that such code is likely to be a bug (parameter name was
> forgotten) and compiler should report about this.
I care a lot about removing bug-prone situations from D language. And I think C# language acts as you ask. Generally C# designers are very careful in avoiding bug-prone features.
But in general it's not so common to forget to add a name for the argument. And even in the cases where I have forgotten to put the argument name, this is usually not a big problem, because the compiler doesn't find the name you are using inside the function, and gives you a nice compilation error at the first usage attempt, close enough to the function signature (unless your functions are really long).
In theory one bug-prone situation is when you use a name thinking it's the name of a local argument, while you have not given a name to the argument, and you have a name in an outer scope that is the same. So you are actually using the name of the outer scope. In practice my diary of bugs shows I have never hit this bug, also because it's generally a good practice to not define local names equal to outer names.
So unless you have evidence that this is a common enough bug (even few anecdotes are enough to keep this discussion open) I think this bug report should be closed.
> BTW dmd doesn't produce any warning for unused variable.
I know, but probably someday some D compiler will report them. It's an useful feature and it's not too much hard to implement. All the major C++ compilers have an option to report them.
But note here I was not referring to a generic "unused variable" warning, I was referring to more specifically to a "unused argument" warning. If you omit the argument name, the warning is silenced.
Comment #4 by maxim — 2012-09-10T05:22:14Z
I have no desire for disputes about things which are not necessarily defective (as revealed here) and claimed by D people to be intentional part of the language.