Global variables read at compile time?
Leandro Motta Barros
lmb at stackedboxes.org
Wed Aug 15 07:21:06 PDT 2012
Another option is to use "module constructors", as shown below. (But
somehow this all looks a bit fishy for me...)
LMB
----
import std.stdio;
string a = "a";
string b;
static this()
{
b = a;
}
void main()
{
writeln(b);
}
On Wed, Aug 15, 2012 at 11:03 AM, d_follower <d_follower at fakemail.com> wrote:
> On Wednesday, 15 August 2012 at 13:36:26 UTC, Stefan wrote:
>>
>> Hi there, I'm having trouble getting the following code to compile:
>>
>> import std.stdio;
>>
>> string a = "a";
>> string b = a; // line 4
>>
>> void main()
>> {
>> writeln(b); // line 8
>>
>> }
>>
>> DMD spits out the error "test.d(4): Error: variable a cannot be read at
>> compile time". Is there any way to tell the compiler I want b evaluated at
>> runtime, or am I missing something obvious here?
>
>
> You must understand that your problem lies in line 4, not in line 8, i.e.
> the following doesn't work either:
>
>
> string a = "a";
> string b = a;
>
> I don't really know why, but it seems that you can only initialize globals
> with constants.
>
> What you could do is something like this (I guess):
>
> enum value = "a";
> string a = value;
> string b = value;
>
> void main()
> {
> writeln(b);
> b = "b";
> writeln(b);
> }
More information about the Digitalmars-d-learn
mailing list