www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - forward reference hell!

reply sa <12 abc.com> writes:
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?
Feb 14 2009
parent reply Moritz Warning <moritzwarning web.de> writes:
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. :(
Feb 14 2009
next sibling parent hehe <hehe hehe.com> writes:
 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!
Feb 14 2009
prev sibling parent Chris R Miller <lordsauronthegreat gmail.com> writes:
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.
Feb 15 2009