www.digitalmars.com         C & C++   DMDScript  

D - Circular Imports

reply Deja Augustine <Deja_member pathlink.com> writes:
I was curious what happens in the following situation:

a.d
---
import b;

class A
{
B x;
}

---
b.d
---
import a;

class B
{
A x;
}


it didn't generate any compile-time errors, so I'm wondering what exactly
happens with it.
Mar 18 2003
parent reply Burton Radons <loth users.sourceforge.net> writes:
Deja Augustine wrote:
 I was curious what happens in the following situation:
 
 a.d
 ---
 import b;
 
 class A
 {
 B x;
 }
 
 ---
 b.d
 ---
 import a;
 
 class B
 {
 A x;
 }
 
 
 it didn't generate any compile-time errors, so I'm wondering what exactly
 happens with it.

D, unlike C, splits semantic phases into a set of stages. In the initial parsing it sees B and A fields in one and the other and doesn't try to interpret it; it's only in the semantic phases that it links them together. It should also be possible to do this: class B : A { } class A { } And this: class A { B x; } struct B { } And this: A x = { 1, 2 }; struct A { int a, b; }
Mar 18 2003
parent reply "Deja Augustine" <DejaD scratch-ware.net> writes:
"Burton Radons" <loth users.sourceforge.net> wrote in message
news:b595vf$25cs$1 digitaldaemon.com...
 Deja Augustine wrote:
 I was curious what happens in the following situation:

 a.d
 ---
 import b;

 class A
 {
 B x;
 }

 ---
 b.d
 ---
 import a;

 class B
 {
 A x;
 }


 it didn't generate any compile-time errors, so I'm wondering what


 happens with it.

D, unlike C, splits semantic phases into a set of stages. In the initial parsing it sees B and A fields in one and the other and doesn't try to interpret it; it's only in the semantic phases that it links them together. It should also be possible to do this: class B : A { } class A { } And this: class A { B x; } struct B { } And this: A x = { 1, 2 }; struct A { int a, b; }

Yeah, that's not really what I was asking. What I wanted to know was, can someone, using my initial example, write a chunk of code like so: ----- c.d ----- import a; B var; int main() { var.x = new A(); var.x.x = new B(); var.x.x.x = new A(); } on into infinity
Mar 18 2003
parent reply Burton Radons <loth users.sourceforge.net> writes:
Deja Augustine wrote:
 Yeah, that's not really what I was asking.  What I wanted to know was, can
 someone, using my initial example, write a chunk of code like so:
 -----
  c.d
 -----
 import a;
 
 B var;
 
 int main()
 {
   var.x = new A();
   var.x.x = new B();
   var.x.x.x = new A();
 }
 
 on into infinity

Yes, because they're classes. If they were struct, it would be a circular field error.
Mar 18 2003
parent "Deja Augustine" <DejaD scratch-ware.net> writes:
"Burton Radons" <loth users.sourceforge.net> wrote in message
news:b59753$263r$1 digitaldaemon.com...
 Deja Augustine wrote:
 Yeah, that's not really what I was asking.  What I wanted to know was,


 someone, using my initial example, write a chunk of code like so:
 -----
  c.d
 -----
 import a;

 B var;

 int main()
 {
   var.x = new A();
   var.x.x = new B();
   var.x.x.x = new A();
 }

 on into infinity

Yes, because they're classes. If they were struct, it would be a circular field error.

Okay. As a side note treat for anyone reading this, I hope to have DEnv Beta 1.2f out sometime this week.
Mar 18 2003