www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Set global immutables from main/getopt?

reply "Andrew Klaassen" <clawsoon yahoo.com> writes:
I thought it'd be a good idea to set global immutables from args 
passed to main().  When I do this:


import io = std.stdio;

immutable string abc;

void foo() {
   io.writeln(abc);
}

void main(string[] args) {
   abc = args[1];
   foo();
}


...I get the compiler error: "Error: can only initialize const 
member abc inside constructor".  When I do this:


import io = std.stdio;

void foo() {
   io.writeln(abc);
}

void main(string[] args) {
   immutable string abc = args[1];
   foo();
}


...I get the compiler error: "Error: undefined identifier abc".

Is there any workaround, or am I attempting a 
stupidity/impossibility?

Thanks.

Andrew
Jan 28 2014
next sibling parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Tuesday, 28 January 2014 at 17:11:43 UTC, Andrew Klaassen 
wrote:

Naturally, initialize them in constructor :)

http://dpaste.dzfl.pl/620e8c61
Jan 28 2014
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 28 Jan 2014 12:27:02 -0500, Stanislav Blinov  
<stanislav.blinov gmail.com> wrote:

 On Tuesday, 28 January 2014 at 17:11:43 UTC, Andrew Klaassen wrote:

 Naturally, initialize them in constructor :)

 http://dpaste.dzfl.pl/620e8c61
That being said, it *could* be done, since the runtime has access to the command line args. But the runtime would have to provide access to those parameters. But you'd have to do it in a static constructor. -Steve
Jan 28 2014
parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Tuesday, 28 January 2014 at 18:30:37 UTC, Steven Schveighoffer 
wrote:
 On Tue, 28 Jan 2014 12:27:02 -0500, Stanislav Blinov 
 <stanislav.blinov gmail.com> wrote:
 http://dpaste.dzfl.pl/620e8c61
That being said, it *could* be done, since the runtime has access to the command line args. But the runtime would have to provide access to those parameters. But you'd have to do it in a static constructor.
Ahem. There was a link to dpaste :D
Jan 28 2014
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 28 Jan 2014 13:41:24 -0500, Stanislav Blinov  
<stanislav.blinov gmail.com> wrote:

 On Tuesday, 28 January 2014 at 18:30:37 UTC, Steven Schveighoffer wrote:
 On Tue, 28 Jan 2014 12:27:02 -0500, Stanislav Blinov  
 <stanislav.blinov gmail.com> wrote:
 http://dpaste.dzfl.pl/620e8c61
That being said, it *could* be done, since the runtime has access to the command line args. But the runtime would have to provide access to those parameters. But you'd have to do it in a static constructor.
Ahem. There was a link to dpaste :D
Oops! I glossed over that :) I thought the example just showed how a static constructor was required to initialize, I didn't see the access to the program args. -Steve
Jan 28 2014
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/28/2014 10:41 AM, Stanislav Blinov wrote:
 On Tuesday, 28 January 2014 at 18:30:37 UTC, Steven Schveighoffer wrote:
 On Tue, 28 Jan 2014 12:27:02 -0500, Stanislav Blinov
 <stanislav.blinov gmail.com> wrote:
 http://dpaste.dzfl.pl/620e8c61
That being said, it *could* be done, since the runtime has access to the command line args. But the runtime would have to provide access to those parameters. But you'd have to do it in a static constructor.
Ahem. There was a link to dpaste :D
On a side note, dpaste is not associated with newsgroups. It can disappear at any time in the future. I like seeing and showing code in message bodies both for convenience and future availability. :) Ali
Jan 28 2014
parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Tuesday, 28 January 2014 at 18:56:35 UTC, Ali Çehreli wrote:

 On a side note, dpaste is not associated with newsgroups. It 
 can disappear at any time in the future. I like seeing and 
 showing code in message bodies both for convenience and future 
 availability. :)
Awww, but it is so much more convenient. No spurious linebreaks, nice syntax highlighting, built-in compiler... :P But I agree, sometimes it's a real pain when you've searching for something and all you find is dead links.
Jan 28 2014
next sibling parent reply "Andrew Klaassen" <clawsoon yahoo.com> writes:
On Tuesday, 28 January 2014 at 19:02:41 UTC, Stanislav Blinov 
wrote:
 On Tuesday, 28 January 2014 at 18:56:35 UTC, Ali Çehreli wrote:

 On a side note, dpaste is not associated with newsgroups. It 
 can disappear at any time in the future. I like seeing and 
 showing code in message bodies both for convenience and future 
 availability. :)
Awww, but it is so much more convenient. No spurious linebreaks, nice syntax highlighting, built-in compiler... :P But I agree, sometimes it's a real pain when you've searching for something and all you find is dead links.
Cutting and pasting for posterity... :-) import std.stdio; import core.runtime; immutable string abc; void main() { writeln(abc); } shared static this() { alias args = Runtime.args; if (args.length > 1) abc = args[1]; } Fascinating. Any chance someone would be able to add this to http://dlang.org/module.html as an example for people like me who'd have no clue otherwise? Andrew
Jan 28 2014
parent "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Tuesday, 28 January 2014 at 20:07:25 UTC, Andrew Klaassen 
wrote:

 Fascinating.  Any chance someone would be able to add this to 
 http://dlang.org/module.html as an example for people like me 
 who'd have no clue otherwise?
Initialization of immutable globals is actually in the docs: http://dlang.org/const3.html. It's a third example in "Immutable Storage Class". ;)
Jan 28 2014
prev sibling parent reply "Meta" <jared771 gmail.com> writes:
On Tuesday, 28 January 2014 at 19:02:41 UTC, Stanislav Blinov 
wrote:
 On Tuesday, 28 January 2014 at 18:56:35 UTC, Ali Çehreli wrote:

 On a side note, dpaste is not associated with newsgroups. It 
 can disappear at any time in the future. I like seeing and 
 showing code in message bodies both for convenience and future 
 availability. :)
Awww, but it is so much more convenient. No spurious linebreaks, nice syntax highlighting, built-in compiler... :P But I agree, sometimes it's a real pain when you've searching for something and all you find is dead links.
You can also use the Github Gist feature for displaying code, can't you?
Jan 28 2014
parent reply "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Wednesday, 29 January 2014 at 00:28:29 UTC, Meta wrote:

 You can also use the Github Gist feature for displaying code, 
 can't you?
But you'd need a github account for that, won't you? :)
Jan 29 2014
parent "anonymous" <anonymous example.com> writes:
On Wednesday, 29 January 2014 at 10:15:03 UTC, Stanislav Blinov
wrote:
 On Wednesday, 29 January 2014 at 00:28:29 UTC, Meta wrote:

 You can also use the Github Gist feature for displaying code, 
 can't you?
But you'd need a github account for that, won't you? :)
no
Jan 29 2014
prev sibling parent "Dicebot" <public dicebot.lv> writes:
One can always turn global into pointer to immutable - it will 
preserve most benefits but can be always replaced with 
differently initialized one.
Jan 29 2014