www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static variable A cannot be read at compile time

reply Newbie2019 <newbie2019 gmail.com> writes:
https://run.dlang.io/is/s4cfiv

onlineapp.d(23): Error: static variable A cannot be read at 
compile time
onlineapp.d(23): Error: cannot implicitly convert expression 1 of 
type int to int*

I see no reason the code should not work,  and the second error 
message make no sense.

please suggestion how should I made this code to work.
Jun 12 2019
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Jun 12, 2019 at 01:12:58PM +0000, Newbie2019 via Digitalmars-d-learn
wrote:
 https://run.dlang.io/is/s4cfiv
 
 onlineapp.d(23): Error: static variable A cannot be read at compile time
 onlineapp.d(23): Error: cannot implicitly convert expression 1 of type int
 to int*
 
 I see no reason the code should not work,  and the second error message make
 no sense.
Read: https://wiki.dlang.org/User:Quickfur/Compile-time_vs._compile-time T -- A mathematician is a device for turning coffee into theorems. -- P. Erdos
Jun 12 2019
parent reply Newbie2019 <newbie2019 gmail.com> writes:
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. Still the last error message make no sense here.
Jun 12 2019
parent Machine Code <jckj33 gmail.com> writes:
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 :)
Jun 12 2019