March 19, 2010
According to the documentation,main() must be declared using one of the
following forms:

void main() { ... }
void main(char[][] args) { ... }
int main() { ... }
int main(char[][] args) { ... }

However with the latest D2 compiler I can write:
import std.stdio;

void main (string[] args) {
  writeln("main with string[]");
}

What's the trick?
March 19, 2010
Ludovic A. Wrote:

> According to the documentation,main() must be declared using one of the
> following forms:
> 
> void main() { ... }
> void main(char[][] args) { ... }
> int main() { ... }
> int main(char[][] args) { ... }

If you use "int main" then you have to add a return, possibly returning std.c.stdlib.EXIT_SUCCESS or std.c.stdlib.EXIT_FAILURE. If you use void it uses a EXIT_SUCCESS.


> However with the latest D2 compiler I can write:
> import std.stdio;
> 
> void main (string[] args) {
>   writeln("main with string[]");
> }

In D1 Tango doesn't define the alias string that's char[]. But the (string[] args) can be used in D1 too, with Phobos: http://codepad.org/wUC6N6LB

In D2 string means something different, it's immutable(char)[] that is a mutable array of immutable chars. I hope Tango2 will define string the same way.
So in D1 (string[] args) is the same as (char[][] args), but in D2 they are a little different things. Even if D2 accepts both forms, I think that modifying the contents of args is not a good practice, so in D2 I suggest to use (string[] args), that has immutable chars.
In practice in D2 you can even use main(immutable string[] args), that I think is the best form :-)

Bye,
bearophile
March 19, 2010
Ok this is what I thought. I filled an issue.
March 19, 2010
On Fri, 19 Mar 2010 06:23:36 -0400, bearophile <bearophileHUGS@lycos.com> wrote:

> So in D1 (string[] args) is the same as (char[][] args), but in D2 they are a little different things. Even if D2 accepts both forms, I think that modifying the contents of args is not a good practice, so in D2 I suggest to use (string[] args), that has immutable chars.
> In practice in D2 you can even use main(immutable string[] args), that I think is the best form :-)

You are allowed to modify the args, they are guaranteed not to be in ROM.  The true signature, if there existed such a thing, should be unique(char[])[] args.  Because D arrays are safe, you are in no danger of corrupting memory, unlike C.

However, the docs should be updated to add the string[] variant, I agree with that.

-Steve
March 19, 2010
Steven Schveighoffer:
> You are allowed to modify the args, they are guaranteed not to be in ROM. The true signature, if there existed such a thing, should be unique(char[])[] args.  Because D arrays are safe, you are in no danger of corrupting memory, unlike C.

In D2 all strings are immutable(somechartype)[], so how can you justify the args[x] to be an exception to that rule?

Bye,
bearophile
March 19, 2010
On Fri, 19 Mar 2010 08:43:25 -0400, bearophile <bearophileHUGS@lycos.com> wrote:

> Steven Schveighoffer:
>> You are allowed to modify the args, they are guaranteed not to be in ROM.
>> The true signature, if there existed such a thing, should be
>> unique(char[])[] args.  Because D arrays are safe, you are in no danger of
>> corrupting memory, unlike C.
>
> In D2 all strings are immutable(somechartype)[], so how can you justify the args[x] to be an exception to that rule?

All string *literals* are immutable.  The reason is so the runtime does not have to use the heap every time you use a string literal.  You can certainly have/use mutable strings.  program arguments are runtime decided, and are passed in one place, so they do not have to be immutable.  Making them immutable, while possible and preferred by some, is not a requirement, and shouldn't be forced on the programmer.

-Steve
Top | Discussion index | About this forum | D home