Wish: Variable Not Used Warning

Koroskin Denis 2korden at gmail.com
Wed Jul 9 03:48:38 PDT 2008


On Wed, 09 Jul 2008 14:05:21 +0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Koroskin Denis:
>> Moreover, I would be happy to have an `unused` modifier in addition to  
>> in,
>> out and inout (doh!) to denote that a variable is not going to be used.  
>> In
>> this case compiler will show an error if the variable is used by chance.
>> It could help programmer to catch potential bugs at early stage once he
>> eventually start using it. Besides, it really fits well into D, IMO:
>> void bar( unused int foo ) // no warning is generated
>> {
>> }
>
> Can you explain me in what practical situation(s) this can be useful?
>
> Bye,
> bearophile

It is the most useful if warning is generated when a variable is unused:

class Connection
{
    void connect(int timeout)
    {
        // do something
    }
}

class SomeOtherConnectionType : Connection
{
    void connect(unused int timeout)
    {
        // this type of connection is immediate, and therefore there is no  
need for timeout
        // but since we don't use timeout, just mark it unused

        // do the connection
    }
}

And then you realize that due to some specific changes this type of  
connection is no more immediate,
so now you are going to take it into account. And you see an unused  
modifier that says to you:

"Man, this variable was not used before, go check your code to see maybe  
there are some cases when you passed some dummy value to this function  
just to satisfy compilator, like this:

auto connection = new SomeOtherConnectionType();
connection.connect(0); // I don't care, since it is immediate anyway

and then refactor your code".



More information about the Digitalmars-d mailing list