www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Module import incompatibility

reply frame <frame86 live.com> writes:
I'm sure it was asked before but can't find the thread:

How to deal best with an older library that uses the same class 
name as module name?
I get a lot of

`Error: module ABC from file ...ABC.d must be imported with 
'import ABC'`

Do I need to rename all modules?
Aug 31 2021
parent reply bauss <jj_1337 live.dk> writes:
On Tuesday, 31 August 2021 at 12:26:28 UTC, frame wrote:
 I'm sure it was asked before but can't find the thread:

 How to deal best with an older library that uses the same class 
 name as module name?
 I get a lot of

 `Error: module ABC from file ...ABC.d must be imported with 
 'import ABC'`

 Do I need to rename all modules?
The problem is that the file doesn't have a module statement so the compiler tries to resolve the module name from the import but is unable to resolve that properly. The fix here is the module must have a module statement but since it's a library then you probably need to go into said library and fix it manually. There's really no other fix I believe.
Aug 31 2021
parent reply frame <frame86 live.com> writes:
On Tuesday, 31 August 2021 at 12:37:51 UTC, bauss wrote:
 On Tuesday, 31 August 2021 at 12:26:28 UTC, frame wrote:
 I'm sure it was asked before but can't find the thread:

 How to deal best with an older library that uses the same 
 class name as module name?
 I get a lot of

 `Error: module ABC from file ...ABC.d must be imported with 
 'import ABC'`

 Do I need to rename all modules?
The problem is that the file doesn't have a module statement so the compiler tries to resolve the module name from the import but is unable to resolve that properly.
No, it has one, eg: ```d module cairo.PdfSurface; ``` but the filename is PdfSurface.d and class name also :\
Aug 31 2021
next sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Tuesday, 31 August 2021 at 12:57:33 UTC, frame wrote:
 No, it has one, eg:

 ```d
 module cairo.PdfSurface;
 ```

 but the filename is PdfSurface.d and class name also :\
You can use a selective import: ```d import cairo.PdfSurface: PdfSurface; ```
Aug 31 2021
parent frame <frame86 live.com> writes:
On Tuesday, 31 August 2021 at 13:03:54 UTC, Paul Backus wrote:
 On Tuesday, 31 August 2021 at 12:57:33 UTC, frame wrote:
 No, it has one, eg:

 ```d
 module cairo.PdfSurface;
 ```

 but the filename is PdfSurface.d and class name also :\
You can use a selective import: ```d import cairo.PdfSurface: PdfSurface; ```
Damn it's working - I swear I tried that before. I believe sometimes VsCode doesn't really updating my files. Thx.
Aug 31 2021
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 8/31/21 8:57 AM, frame wrote:
 On Tuesday, 31 August 2021 at 12:37:51 UTC, bauss wrote:
 On Tuesday, 31 August 2021 at 12:26:28 UTC, frame wrote:
 I'm sure it was asked before but can't find the thread:

 How to deal best with an older library that uses the same class name 
 as module name?
 I get a lot of

 `Error: module ABC from file ...ABC.d must be imported with 'import 
 ABC'`

 Do I need to rename all modules?
The problem is that the file doesn't have a module statement so the compiler tries to resolve the module name from the import but is unable to resolve that properly.
No, it has one, eg: ```d module cairo.PdfSurface; ``` but the filename is PdfSurface.d and class name also :\
Are you sure this is the problem? `PdfSurface` is not a valid identifier here except for the class. In order to access the package, you need to use `cairo.PdfSurface`. Tango was full of stuff like this, and it worked fine *as long as* it wasn't a top-level module. -Steve
Aug 31 2021
next sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Tuesday, 31 August 2021 at 14:09:01 UTC, Steven Schveighoffer 
wrote:
 Are you sure this is the problem? `PdfSurface` is not a valid 
 identifier here except for the class. In order to access the 
 package, you need to use `cairo.PdfSurface`.
Must've changed since you last checked: ```d // main.d import example; void main() { // Error: incompatible types for `(module example) == (42)`: `void` and `int` assert(example == 42); } ``` ```d // example.d int example = 42; ``` https://run.dlang.io/is/SpEZNF
Aug 31 2021
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 8/31/21 11:17 AM, Paul Backus wrote:
 On Tuesday, 31 August 2021 at 14:09:01 UTC, Steven Schveighoffer wrote:
 Are you sure this is the problem? `PdfSurface` is not a valid 
 identifier here except for the class. In order to access the package, 
 you need to use `cairo.PdfSurface`.
Must've changed since you last checked: ```d // main.d import example; void main() {     // Error: incompatible types for `(module example) == (42)`: `void` and `int`     assert(example == 42); } ``` ```d // example.d int example = 42; ``` https://run.dlang.io/is/SpEZNF
That is a top-level module that has an equivalent name inside. I did say "*as long as* it wasn't a top-level module" ```d // main.d import pkg.mod; void main() { assert mod = 42; } // example.d module pkg.mod; int mod = 42; ``` works fine https://run.dlang.io/is/sQ4Bsa This is why I always try to use a top-level package instead of a module (especially with a common name). -Steve
Aug 31 2021
prev sibling parent frame <frame86 live.com> writes:
On Tuesday, 31 August 2021 at 14:09:01 UTC, Steven Schveighoffer 
wrote:

 Are you sure this is the problem? `PdfSurface` is not a valid 
 identifier here except for the class. In order to access the 
 package, you need to use `cairo.PdfSurface`.

 Tango was full of stuff like this, and it worked fine *as long 
 as* it wasn't a top-level module.

 -Steve
Ah, now I see the problem. It was imported with an additional top-level package path and that caused my confusion because the compiler shows the right path in the error and also the IDE sees the package with this top-level path, so it seems right but the module declaration doesn't match, of course. Thanks.
Aug 31 2021