Thread overview
forward reference hell!
Feb 15, 2009
sa
Feb 15, 2009
Moritz Warning
Feb 15, 2009
hehe
Feb 15, 2009
Chris R Miller
February 15, 2009
Since there are more people here, I fwd the post here to get advice.

=============================================== http://d.puremagic.com/issues/show_bug.cgi?id=2666

$ cat vector.d #----------------------------------------------------- module vector;

class Vector(E) {
  E[] data;
}

$ cat student.d #----------------------------------------------------- module student;

import vector;
import teacher;

// define Student
class Student {
  void ask(Teacher teacher) {
  }
}

// define Students
alias Vector!(Student) Students;

$ cat teacher.d #----------------------------------------------------- module teacher;

import student;

class Teacher {
  Students students;
}

===============================================
$ dmd -c student.d
teacher.d(6): Error: forward reference to 'Vector!(Student)'
teacher.d(6): Error: Students is used as a type
teacher.d(6): variable teacher.Teacher.students voids have no value

===============================================
sure I know if I do:
$ dmd -c vector.d  teacher.d student.d
all the three files can be compiled without error.

But my question is: why can't file be individually compiled?

I think I have the most natural/logical organization of files; if you move the 'Students' alias around, it could get compiled, but what's wrong with my current organization?

February 15, 2009
On Sun, 15 Feb 2009 01:19:54 +0000, sa wrote:

> Since there are more people here, I fwd the post here to get advice.
> 
> =============================================== http://d.puremagic.com/issues/show_bug.cgi?id=2666
> 
> $ cat vector.d #----------------------------------------------------- module vector;
> 
> class Vector(E) {
>   E[] data;
> }
> 
> $ cat student.d #----------------------------------------------------- module student;
> 
> import vector;
> import teacher;
> 
> // define Student
> class Student {
>   void ask(Teacher teacher) {
>   }
> }
> 
> // define Students
> alias Vector!(Student) Students;
> 
> $ cat teacher.d #----------------------------------------------------- module teacher;
> 
> import student;
> 
> class Teacher {
>   Students students;
> }
> 
> =============================================== $ dmd -c student.d
> teacher.d(6): Error: forward reference to 'Vector!(Student)'
> teacher.d(6): Error: Students is used as a type teacher.d(6): variable
> teacher.Teacher.students voids have no value
> 
> =============================================== sure I know if I do: $ dmd -c vector.d  teacher.d student.d all the three files can be compiled without error.
> 
> But my question is: why can't file be individually compiled?
> 
> I think I have the most natural/logical organization of files; if you move the 'Students' alias around, it could get compiled, but what's wrong with my current organization?

fwiw, I was porting some pre D1.0 D program (not mine) to D1.0 today
and run into the same problem.
I wasted >2 hours to play with dependencies just to end up with no
solution.
Looks like I have to pull ~8 files into one just to make it compile,
but ruin code separation. :(
February 15, 2009
> fwiw, I was porting some pre D1.0 D program (not mine) to D1.0 today
> and run into the same problem.
> I wasted >2 hours to play with dependencies just to end up with no
> solution.
> Looks like I have to pull ~8 files into one just to make it compile,
> but ruin code separation. :(

Haha, you are going thru the same route as I did years ago, and when I eventually put them all into one file (plus generated code), and make the compiler happy, the linker won't link it because there are more than 16,000 fixups in one object file, see the linker bug:

http://d.puremagic.com/issues/show_bug.cgi?id=424

I have to say: good luck!
February 15, 2009
To stop a forward-reference problem you need to declare the type, but not implement it.  Eg:

$ cat vector.d #-----------------------------------------------------
> module vector;
>
> class Vector(E) {
>   E[] data;
> }
>
> $ cat student.d #-----------------------------------------------------
> module student;
>
> import vector;

class Teacher;

> // define Student
> class Student {
>   void ask(Teacher teacher) {
>   }
> }
>
> // define Students
> alias Vector!(Student) Students;
>
> $ cat teacher.d #-----------------------------------------------------
> module teacher;
>
> import student;
>
> class Teacher {
>   Students students;
> }

It is not necessary to smash them all into one file.