www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Very very noobie question about how imports work.

reply J Smith <jamalroycesmith gmail.com> writes:
Say I have a project with the files structured like this.

package-name/
     src/
         package-name/
             lib.d
     test/
         testlib.d

How do I make it so that I can import and use the contents of 
lib.d inside of testlib.d.

As you can tell I'm very new to D, and kind of new to 
programming. Forgive me for very noob and easy question, but 
couldn't really find anything out by reading the docs.
Dec 10 2015
next sibling parent reply tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
On Friday, 11 December 2015 at 03:20:29 UTC, J Smith wrote:
 Say I have a project with the files structured like this.

 package-name/
     src/
         package-name/
             lib.d
     test/
         testlib.d

 How do I make it so that I can import and use the contents of 
 lib.d inside of testlib.d.

 As you can tell I'm very new to D, and kind of new to 
 programming. Forgive me for very noob and easy question, but 
 couldn't really find anything out by reading the docs.
In D, directory structure doesn't matter. What matters is module names. Let's say module name of lib.d is "module a;", and for testlib.d, it is "module b;". Then you can access whichever you want with dot notation. "a.foo();", "b.var = 5;" etc. Where directory structure matters is compiling. dmd main.d src/package-name/lib.d test/testlib.d Because it is logical to match directory structure and module name, it is done in that way mostly, but there are times you might not want this.
Dec 10 2015
parent Mike Parker <aldacron gmail.com> writes:
On Friday, 11 December 2015 at 03:51:35 UTC, tcak wrote:

 In D, directory structure doesn't matter. What matters is 
 module names.
Actually, it does matter sometimes. // src/foo/bar.d module foo.oops; // main.d import foo.oops; void main() {} Compile: dmd -Isrc main.d Result: main.d(1): Error: module oops is in file 'foo\oops.d' which cannot be read Compiling them together works: dmd main.d src/foo/bar.d When you pass multiple source modules on the command line, the compiler will determine the name of each module. If there is no module statement, it is the file name (but without the path -- it's in the default package). Otherwise, it's whatever is specified by the module statement. This name will be added to the list of available modules. When compilation begins, each import statement is checked against the list of available modules. If the imported module is not found, the compiler will look for it on the import path. It will convert the fully qualified name of the imported module into a file path. In the first command line above, when bar.d is not passed along, the compiler finds import foo.oops and searches the import path for foo/oops.d. Since that file does not exist, we get the error. Change the module statement in bar.d to 'module foo.bar' and the compiler will find it, since the src directory is added to the import path with -I. In the second case, when foo.bar.d is passed to the command line, the compiler finds its module statement, 'module foo.oops' and adds 'foo.oops' to the list of available modules. In this case, the file name is irrelevant and compilation succeeds.
 Because it is logical to match directory structure and module 
 name, it is done in that way mostly, but there are times you 
 might not want this.
Module names should *always* match the file path unless you have a very compelling reason not to do so. As shown above, it's not just because it's logical, but because compilation can break when modules are compiled separately.
Dec 11 2015
prev sibling parent reply Chris Wright <dhasenan gmail.com> writes:
On Fri, 11 Dec 2015 03:20:29 +0000, J Smith wrote:

 How do I make it so that I can import and use the contents of lib.d
 inside of testlib.d.
If you are not compiling everything in one step, the -I flag allows you to specify paths to look for imports. For instance: $ dmd -lib src/package_name/lib.d $ dmd -Isrc test/testlib.d libpackage_name.a
Dec 10 2015
parent tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
On Friday, 11 December 2015 at 04:09:19 UTC, Chris Wright wrote:
 On Fri, 11 Dec 2015 03:20:29 +0000, J Smith wrote:

 How do I make it so that I can import and use the contents of 
 lib.d inside of testlib.d.
If you are not compiling everything in one step, the -I flag allows you to specify paths to look for imports. For instance: $ dmd -lib src/package_name/lib.d $ dmd -Isrc test/testlib.d libpackage_name.a
Whops. I re-read the post, and yeah, it is for other import.
Dec 10 2015