How compiler detects forward reference errors

Igor via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Sep 4 12:15:15 PDT 2016


On Saturday, 3 September 2016 at 14:13:27 UTC, Lodovico Giaretta 
wrote:
> On Saturday, 3 September 2016 at 14:06:06 UTC, Igor wrote:
>> Can anyone explain in plain English how does compiler process 
>> and detect a "test.d(6) Error: forward reference of variable 
>> a" in following code:
>>
>> import std.stdio;
>>
>> enum a = 1 + b;
>> enum d = 5 + a; // No error here
>> enum b = 12 + c;
>> enum c = 10 + a; // error here
>>
>> void main()
>> {
>>     writeln("Hello World!", b);
>> }
>
> a needs b to be initialized. So b must be initialized before a. 
> Let's write this b->a. Now b needs c. So c->b. c needs a, so 
> a->c. If we sum everything, we have that a->c->b->a. This mean 
> that to initialize a we need b, to initialize b we need c, but 
> to initialize c we need a. So to initialize a we need a, which 
> is not possible. We need a before having initialized it.
>
> On the other hand, a->d is not a problem, as d can be 
> initialized after a.

So, you are saying compiler is keeping a kind of linked list of 
dependencies and then checks if any of those lists are circular? 
But how exactly would that list be structured since one 
expression can have multiple dependencies, like:

enum a = b + c + d + e;
enum b = 10 + c;
enum c = d + e + a;
...


More information about the Digitalmars-d-learn mailing list