www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem with imports in class hierarchy

reply Marco Falda <Marco_member pathlink.com> writes:
I find a strange error when trying to compile the simple class hierarchy
reported below, in particular I am not able to compile A11.d nor A2.d; the
compiler tells me: "A1.d(3): import A1.A conflicts with A2.A at A2.d(3)".
Notice that I need the class A both in A1 and A2 (in C++ this could be declared
as a virtual base class); in my opinion the problem is in A11 (in fact is the
class that "glues" all the code).
What is the problem? How can I fix it maintaining the hierarchy?

/* file: A.d */

module A;

class A {}

---------------
/* file: A1.d */

module A1;

private import A;

class A1 : A {}

---------------
/* file: A11.d */

module A11;

private import A1;
private import A2; // needed by A11

class A11 : A1 {

this(A i) {}

}

---------------
/* file: A2.d */

module A2;

private import A;
private import A11;  // needed by A2

class A2 : A {}
Apr 26 2005
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
Marco Falda <Marco_member pathlink.com> wrote:

[...]
 "A1.d(3): import A1.A conflicts with A2.A at A2.d(3)"

 this(A i) {}

The reported error message is not reproducible by your example. The `A' in the parameter list of the constructor is out of scope, but replacing it by `this(A1 i) {}' clears all error messages. -manfred
Apr 26 2005
parent reply Marco Falda <Marco_member pathlink.com> writes:
The reported error message is not reproducible by your example.

The `A' in the parameter list of the constructor is out of scope, but 
replacing it by
  `this(A1 i) {}'
clears all error messages.

-manfred

I re-checked the code: on WinXP Home SP2 my DMD compiler, version 0.121 (I think, in any case the latest on the site), gives me that error. Anyway, thank you very much for your help, I just added "private import A" in A11.d and now it is all right. I am wondering however why it is not possible to import module A through module A1 in A11 by using a simple "import A" (in A1).
Apr 27 2005
next sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
Marco Falda <Marco_member pathlink.com> wrote:

[...]
 I just added "private import A" in A11.d and now it is all
 right. I am wondering however why it is not possible to import
 module A through module A1 in A11 by using a simple "import A"
 (in A1). 

It is both working: - in a1.d "private import a;" and in a11.d "private import a, a1;" or - in a1.d "import a;" and in a11.d "private import a1;" -manfred
Apr 27 2005
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Marco Falda" <Marco_member pathlink.com> wrote in message 
news:d4nime$j36$1 digitaldaemon.com...
 I am wondering however why it is not possible to import module A through 
 module
 A1 in A11 by using a simple "import A" (in A1).

You can, but it's bad practice. Whenever making modules that will be imported, always use private import to import other modules into them. Otherwise, it's very easy to pollute the namespace with names you don't need.
Apr 27 2005