www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - module import rules

reply "module" <crayolist gmail.com> writes:
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
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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
parent reply "module" <crayolist gmail.com> writes:
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
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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
parent "module" <crayolist gmail.com> writes:
I see, that makes sense! thanks
Feb 25 2015