static variable A cannot be read at compile time

Machine Code jckj33 at gmail.com
Wed Jun 12 18:31:14 UTC 2019


On Wednesday, 12 June 2019 at 14:09:08 UTC, Newbie2019 wrote:
> On Wednesday, 12 June 2019 at 13:53:09 UTC, H. S. Teoh wrote:
>> On Wed, Jun 12, 2019 at 01:12:58PM +0000, Newbie2019 via 
>> Digitalmars-d-learn wrote:
>> Read: 
>> https://wiki.dlang.org/User:Quickfur/Compile-time_vs._compile-time
>>
>>
>> T
>
>
> Thanks for the tips.
>
> I move the static array into local var then it work.

You keep a static variable then initilizae it at module's static 
this() { },

something like this:


struct Arguments {
         int     verbose;
}
__gshared Arguments A;

__gshared option[1] long_options;

static this()
{
      long_options =  [option(cast(char*)("verbose".ptr), 
no_argument,  &A.verbose, 1)];
}


> Still the last error message make no sense here.

It's the call to struct constructor that you're missing. Those 
C-style array initialziation doesn't work with D (as far I know) 
so that you have to explicit (and I like it, even if C-style did 
work in D, I wouldn't use) so you have to call the constructor as 
I did in below code. Anothing type error is: "verbose" is of D's 
string type, not a raw pointer as in C and you need in your 
struct member. To get that, use D's .ptr property then cast away 
immutable to get the const char* you need.

I guess you're implementing a command-line parsing? if you aren't 
aware of, D has something in its standard library for that: 
https://dlang.org/phobos/std_getopt.html might be helpful.

That being said, if you find tedious to build an array with 
default values for specific struct members, you can automate all 
that with D CTFE or just use derived struct with default members. 
Things here do not need to be like C at high-level :)


More information about the Digitalmars-d-learn mailing list