Thread overview
module import rules
Feb 25, 2015
module
Feb 25, 2015
Adam D. Ruppe
Feb 25, 2015
module
Feb 25, 2015
Adam D. Ruppe
Feb 25, 2015
module
February 25, 2015
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!


February 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.
February 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!
February 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.
February 25, 2015
I see, that makes sense! thanks