const main args?

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Aug 12 17:10:35 PDT 2011


On 8/13/11, bearophile <bearophileHUGS at lycos.com> wrote:
> Andrej Mitrovic:
>
>> void main(string[] args)
>> {
>>     args.popFront;  // get rid of name
>>
>>     foreach (arg; args)
>>     {
>>     }
>> }
>
> If I see code like that I probably rewrite it like this:
>
> void main(in string[] args) {
>     foreach (arg; args[1..$]) {
>     }
> }
>

And what benefit is there to that? I pop the argument and then don't
have to worry about it ever again. Whereas you're using [1..$], and
later on in your argument parsing code you might introduce a bug like:

auto userArgumentCount = args.length; // woops, you'll get 1 extra
here due to app name.


More information about the Digitalmars-d-learn mailing list