module vs import
Tomas Lindquist Olsen
tomas at famolsen.dk
Tue Jun 10 03:17:10 PDT 2008
Tomasz Sowiñski wrote:
> Robert Fraser Wrote:
>
>> The "module" keyword is used to assign a name to your module (which
>> incidentally must coincide with its filename & folder structure) while
>> "import" gives you access to the module with the given name.
>
> By the way, why do you have to repeat the filename in the file itself? I'm sure there must be some reason, but I can't see it now.
>
> Tomek
First, the module identifier *must* not coincide with the file path.
One case where this is useful, and not mentioned in the other replies (I think), is when a
module can have multiple implementations. The module that imports find could be a .di file, for
example foobar.di, while the actual implementation .d files could have other file names, for
example foobar_win32.d, foobar_linux.d etc, here the module identifier is necessary to mangle
symbols correctly.
If you're using Makefiles or simple scripts to build your software this is an entirely feasible
setup!
Of course D has the 'version' keyword for this, but sometimes the code can be very big and
seperate files might be more maintainable. I personally prefer switching my editor to another
file than scrolling a hundreds of lines to look at the other implementation.
Tomas
More information about the Digitalmars-d-learn
mailing list