Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 15, 2012 Global variables read at compile time? | ||||
---|---|---|---|---|
| ||||
Hi there, I'm having trouble getting the following code to compile: import std.stdio; string a = "a"; string b = a; void main() { writeln(b); } 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? |
August 15, 2012 Re: Global variables read at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan | 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;
>
> void main()
> {
> writeln(b);
> }
>
> 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?
Make a an enum, const or otherwise immutable.
|
August 15, 2012 Re: Global variables read at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to RommelVR | On Wednesday, 15 August 2012 at 13:41:10 UTC, RommelVR wrote:
> Make a an enum, const or otherwise immutable.
I don't think you understood the question.
|
August 15, 2012 Re: Global variables read at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan | 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);
}
|
August 15, 2012 Re: Global variables read at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan | 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; > > void main() > { > writeln(b); > } > > 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? untested string a = "a"; static this() { string b = a; } be aware of the details: http://dlang.org/module.html#staticorder |
August 15, 2012 Re: Global variables read at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to d_follower | On 8/15/12, d_follower <d_follower@fakemail.com> wrote: > I don't really know why, but it seems that you can only initialize globals with constants. That's what the static constructor is for: http://dlang.org/class.html#StaticConstructor http://dlang.org/class.html#SharedStaticConstructor |
August 15, 2012 Re: Global variables read at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan | On Wed, 15 Aug 2012 15:36:24 +0200, Stefan wrote:
> Hi there, I'm having trouble getting the following code to compile:
>
> import std.stdio;
>
> string a = "a";
> string b = a;
>
> void main()
> {
> writeln(b);
> }
>
> 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?
D is not as context-sensitive as what you may be used to. This is a feature to make the language more parseable and human-grokable. Basically, the result of a module-level assignment should not depend on what came before, so this example code is invalid:
string a = "a";
a = "b";
string b = a;
This would require the declaration of b to depend on the order of the declarations/statements which come before it. When you add in the import of modules, things would get really hairy really fast. So module-level declarations must use constant expressions (literals or symbols to constant data) in their initialization.
Justin
|
August 15, 2012 Re: Global variables read at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to d_follower | On 08/15/2012 06:55 AM, d_follower wrote: > On Wednesday, 15 August 2012 at 13:41:10 UTC, RommelVR wrote: >> Make a an enum, const or otherwise immutable. > > I don't think you understood the question. I thought RommelVR did understand the question. Try this: import std.stdio; enum a = "a"; string b = a; void main() { writeln(b); } Ali -- D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html |
August 15, 2012 Re: Global variables read at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to d_follower | 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@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);
> }
|
May 06, 2014 Re: Global variables read at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | I have got same error. I need to pass in instance of class constant, but got error "Error: static variable cannot be read at compile" http://www.everfall.com/paste/id.php?1mc9mb9cxyie When I had create instance of class in main, and create confvarible above it all worked, but when I had moved it's in module I got error. |
Copyright © 1999-2021 by the D Language Foundation