Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
April 03, 2012 making args global | ||||
---|---|---|---|---|
| ||||
Greetings. imagine this code... void c() { char [] t = args[0]; } void main(char[][] args) { int i = 1; } How can I make args global? thanks, jose |
April 03, 2012 Re: making args global | ||||
---|---|---|---|---|
| ||||
Posted in reply to jicman | On 4/4/12, jicman <cabrera@wrc.xerox.com> wrote:
> imagine this code...
I'm assuming you're using D2.
import core.runtime;
void c()
{
char[] t = Runtime.args[0].dup;
}
void main(char[][] args)
{
int i = 1;
}
.dup is necessary since Runtime keeps the args as a string[] and not a char[][].
|
April 03, 2012 Re: making args global | ||||
---|---|---|---|---|
| ||||
Posted in reply to jicman | On 04/03/2012 03:32 PM, jicman wrote: > > Greetings. > > imagine this code... > > void c() > { > char [] t = args[0]; > } > void main(char[][] args) > { > int i = 1; > } > > How can I make args global? > > thanks, > > jose First, the general discouragement: Avoid global data. :) You can initialize a global variable upon entering main: import std.stdio; string[] g_args; void foo() { writeln("Program name: ", g_args[0]); } void main(string[] args) { g_args = args; foo(); } Ali |
April 03, 2012 Re: making args global | ||||
---|---|---|---|---|
| ||||
Posted in reply to jicman | On 4 April 2012 10:32, jicman <cabrera@wrc.xerox.com> wrote:
> How can I make args global?
>
> thanks,
In D, technically the only way is to use Runtime, as Andrej mentioned.
As an aside, it is worth noting there is no global scope in D, module is as high as you go.
|
April 04, 2012 Re: making args global | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Tue, Apr 03, 2012 at 03:44:08PM -0700, Ali Çehreli wrote: > On 04/03/2012 03:32 PM, jicman wrote: [...] > > How can I make args global? [...] > First, the general discouragement: Avoid global data. :) > > You can initialize a global variable upon entering main: [...] Technically that's a module variable. There's no such thing as global scope in D. :-) T -- Bomb technician: If I'm running, try to keep up. |
April 04, 2012 Re: making args global | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | thanks all. |
Copyright © 1999-2021 by the D Language Foundation