Simple import question

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 9 13:09:20 PDT 2014


On 10/9/14 3:30 PM, Adam D. Ruppe wrote:
> On Thursday, 9 October 2014 at 18:21:32 UTC, WhatMeWorry wrote:
>> "To import one module from another, specify the name of the module in
>> an import declaration. The name must include the relative path
>> computed from the directory where compilation takes place"
>
> This is not true. It is a REALLY common misconception, but it is not
> true. The import name must match the name given in the module
> declaration of the file.
>
> So in the file you're importing, add "module your.name;" to the top. In
> the main file, import it as "import your.name;". If those two don't
> match, it will complain "cannot import module "foo" as "foo.bar"" or
> something like that.

Yes, but in order to read the file, it has to be passed to the compiler, 
or reside at the location dictated by that module name.

This is something quite different from C/C++, because the location in 
the filesystem doesn't matter at all for the compiler, only the 
preprocessor. Here, the file location AND module declaration are important.

>
> It is recommended that the module name match the file name and folder
> name, but it is the module declaration at the top that matters, not the
> file/folder name.

You will encounter great pains to not do this (match folder/file names 
with package/module names).

-Steve


More information about the Digitalmars-d-learn mailing list