digitalmars.D.learn - module import rules
- module (22/22) Feb 25 2015 Hello,
- Adam D. Ruppe (7/7) Feb 25 2015 The module declaration at the top of the file is needed and must
- module (8/8) Feb 25 2015 Thanks Adam for the prompt response;
- Adam D. Ruppe (6/9) Feb 25 2015 That's true but the key thing is it is the filename... which
- module (1/1) Feb 25 2015 I see, that makes sense! thanks
Hello, I have a main.d file which looks like this; import somepackage.somemodule; void main() { printSomething(); } Next to maind.d I have a directory 'somepackage' with a file inside called 'somemodule.d' which looks like this; void printSomething() { import std.stdio : writeln; "Hello".writeln; } Inside the main.d's folder I type; dmd main.d somepackage/somemodule.d and I get the following error message; main.d(1): Error: module somemodule from file somepackage/somemodule.d must be imported with 'import somemodule;' But I'm failing to understand why? Thanks!
Feb 25 2015
The module declaration at the top of the file is needed and must match the import. So in somepackage/somemodule.d, add at the top module somepackage.somemodule; and then it will all work. While the module declaration is technically optional, any file that is imported really should have it every time.
Feb 25 2015
Thanks Adam for the prompt response; I was following the process of importing modules as described in TDPL and Ali's book and they both claim that "By default, the name of a module is the same as its filename without the .d" and continue on to describe how the compiler searches for modules. So my question was not really how to make it work, rather what exactly is wrong with my code. Thanks again!
Feb 25 2015
On Wednesday, 25 February 2015 at 21:14:17 UTC, module wrote:I was following the process of importing modules as described in TDPL and Ali's book and they both claim that "By default, the name of a module is the same as its filename without the .d"That's true but the key thing is it is the filename... which doesn't include the directory name. That's why it mismatches when you start to use packages. You imported it as package.module, but the file name is just module.d, making the automatic module name plain "module", without the attached package/directory name.
Feb 25 2015