www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - conflicting alias in package module

reply 60rntogo <60rntogo gmail.com> writes:
I have a package with the following structure:

pack
|- foo.d
|- bar.d
|- package.d

and the modules look like this:

---
module pack.foo;

struct Foo {}
---
module pack.bar;

import pack : Foo;
---
module pack;

public import pack.foo, pack.bar;
---

and this is an error: "struct pack.foo.Foo at 
source/pack/foo.d(3,1) conflicts with alias pack.bar.Foo at 
source/pack/bar.d(3,8)". I seems like the import in package.d 
sees Foo both in pack.foo and pack.bar, but I don't understand 
why this happens since the import in bar.d is private, isn't it?
Sep 26 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 9/26/20 3:33 AM, 60rntogo wrote:
 I have a package with the following structure:
 
 pack
 |- foo.d
 |- bar.d
 |- package.d
 
 and the modules look like this:
 
 ---
 module pack.foo;
 
 struct Foo {}
 ---
 module pack.bar;
 
 import pack : Foo;
 ---
 module pack;
 
 public import pack.foo, pack.bar;
 ---
 
 and this is an error: "struct pack.foo.Foo at source/pack/foo.d(3,1) 
 conflicts with alias pack.bar.Foo at source/pack/bar.d(3,8)". I seems 
 like the import in package.d sees Foo both in pack.foo and pack.bar, but 
 I don't understand why this happens since the import in bar.d is 
 private, isn't it?
A selective import is equivalent to aliasing (to the public) the symbol as if it were defined in that scope. You have to label it as private if you want it to be private. -Steve
Sep 26 2020
next sibling parent reply 60rntogo <60rntogo gmail.com> writes:
On Saturday, 26 September 2020 at 13:45:21 UTC, Steven 
Schveighoffer wrote:
 A selective import is equivalent to aliasing (to the public) 
 the symbol as if it were defined in that scope.

 You have to label it as private if you want it to be private.
Are you saying that I should write this in bar.d? private import pack : Foo; This still gives the same error.
Sep 26 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 9/26/20 10:59 AM, 60rntogo wrote:
 On Saturday, 26 September 2020 at 13:45:21 UTC, Steven Schveighoffer wrote:
 A selective import is equivalent to aliasing (to the public) the 
 symbol as if it were defined in that scope.

 You have to label it as private if you want it to be private.
Are you saying that I should write this in bar.d? private import pack : Foo; This still gives the same error.
Oh wait, I missed the problem. It's here: pack/bar.d: module pack.bar; import pack : Foo; Here you are importing the PACKAGE, which is importing bar, which is trying to define Foo. It's a recursive loop. Instead: import pack.foo : Foo; Works. -Steve
Sep 26 2020
parent reply 60rntogo <60rntogo gmail.com> writes:
On Saturday, 26 September 2020 at 15:12:47 UTC, Steven 
Schveighoffer wrote:
 Instead:

 import pack.foo : Foo;

 Works.
OK, sure. It's just that the motivation behind doing public imports in package.d is that I can write "import pack" instead of "import pack.foo". I guess it simply doesn't work within the package itself.
Sep 26 2020
parent reply Mike Parker <aldacron gmail.com> writes:
On Saturday, 26 September 2020 at 22:43:10 UTC, 60rntogo wrote:

 OK, sure. It's just that the motivation behind doing public 
 imports in package.d is that I can write "import pack" instead 
 of "import pack.foo". I guess it simply doesn't work within the 
 package itself.
package.d is for your external interface.
Sep 26 2020
parent reply 60rntogo <60rntogo gmail.com> writes:
On Sunday, 27 September 2020 at 03:33:20 UTC, Mike Parker wrote:
 package.d is for your external interface.
Fair enough, thanks.
Sep 26 2020
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 9/27/20 1:17 AM, 60rntogo wrote:
 On Sunday, 27 September 2020 at 03:33:20 UTC, Mike Parker wrote:
 package.d is for your external interface.
Fair enough, thanks.
This isn't an issue with package, it's an issue with a circular dependency. You are importing yourself and trying alias something to itself before that is defined, which has a conflict. -Steve
Sep 27 2020
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 9/26/20 9:45 AM, Steven Schveighoffer wrote:
 On 9/26/20 3:33 AM, 60rntogo wrote:
 and this is an error: "struct pack.foo.Foo at source/pack/foo.d(3,1) 
 conflicts with alias pack.bar.Foo at source/pack/bar.d(3,8)". I seems 
 like the import in package.d sees Foo both in pack.foo and pack.bar, 
 but I don't understand why this happens since the import in bar.d is 
 private, isn't it?
A selective import is equivalent to aliasing (to the public) the symbol as if it were defined in that scope.
Just in case someone comes along and reads this, I was wrong about this. It's not a public import. -Steve
Sep 27 2020