Thread overview
conflicting alias in package module
Sep 26, 2020
60rntogo
Sep 26, 2020
60rntogo
Sep 26, 2020
60rntogo
Sep 27, 2020
Mike Parker
Sep 27, 2020
60rntogo
September 26, 2020
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?
September 26, 2020
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
September 26, 2020
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.

September 26, 2020
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
September 26, 2020
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.

September 27, 2020
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.
September 27, 2020
On Sunday, 27 September 2020 at 03:33:20 UTC, Mike Parker wrote:
> package.d is for your external interface.

Fair enough, thanks.
September 27, 2020
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
September 27, 2020
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