www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - including a file

reply James <james gmail.com> writes:
i created to include file, 1 with 'module xxx' declaration and the other
without it. but i still can import both files. what is the diff here?
Nov 09 2008
next sibling parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Sun, Nov 9, 2008 at 10:18 PM, James <james gmail.com> wrote:
 i created to include file, 1 with 'module xxx' declaration and the other
without it. but i still can import both files. what is the diff here?
Not a lot. The module declaration doesn't serve much purpose. The only things I know it's used for is a place to attach documentation for the module and as a way to make Rebuild shut up (it will whine about the file that doesn't have the module declaration at the top). Oh, and if you put an incorrect declaration on a module (say, it's foo/bar.d but you put "module bar;" instead of "module foo.bar;"), the compiler will sometimes barf. I really am not too sure what it's there for.
Nov 09 2008
next sibling parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Jarrett Billingsley wrote:
 On Sun, Nov 9, 2008 at 10:18 PM, James <james gmail.com> wrote:
 i created to include file, 1 with 'module xxx' declaration and the other
without it. but i still can import both files. what is the diff here?
Not a lot. The module declaration doesn't serve much purpose. The only things I know it's used for is a place to attach documentation for the module and as a way to make Rebuild shut up (it will whine about the file that doesn't have the module declaration at the top). Oh, and if you put an incorrect declaration on a module (say, it's foo/bar.d but you put "module bar;" instead of "module foo.bar;"), the compiler will sometimes barf. I really am not too sure what it's there for.
The module name is used for name mangling. If you have two modules with the same name in different packages you'll get name conflicts. And without module declarations, the compiler assumes a top-level package with a module name derived from the file name. This is a recipe for trouble if you ever have two modules with the same name in different packages.
Nov 10 2008
prev sibling parent reply Christopher Wright <dhasenan gmail.com> writes:
Jarrett Billingsley wrote:
 On Sun, Nov 9, 2008 at 10:18 PM, James <james gmail.com> wrote:
 i created to include file, 1 with 'module xxx' declaration and the other
without it. but i still can import both files. what is the diff here?
Not a lot. The module declaration doesn't serve much purpose. The only things I know it's used for is a place to attach documentation for the module and as a way to make Rebuild shut up (it will whine about the file that doesn't have the module declaration at the top). Oh, and if you put an incorrect declaration on a module (say, it's foo/bar.d but you put "module bar;" instead of "module foo.bar;"), the compiler will sometimes barf. I really am not too sure what it's there for.
If your filename is not a legal identifier, you can make it compile anyway by providing a module statement. I don't know how that works with imports.
Nov 10 2008
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Christopher Wright wrote:
 Jarrett Billingsley wrote:
 On Sun, Nov 9, 2008 at 10:18 PM, James <james gmail.com> wrote:
 i created to include file, 1 with 'module xxx' declaration and the 
 other without it. but i still can import both files. what is the diff 
 here?
Not a lot. The module declaration doesn't serve much purpose. The only things I know it's used for is a place to attach documentation for the module and as a way to make Rebuild shut up (it will whine about the file that doesn't have the module declaration at the top). Oh, and if you put an incorrect declaration on a module (say, it's foo/bar.d but you put "module bar;" instead of "module foo.bar;"), the compiler will sometimes barf. I really am not too sure what it's there for.
If your filename is not a legal identifier, you can make it compile anyway by providing a module statement. I don't know how that works with imports.
How it works with imports? That's easy: it doesn't. :) AFAIK there's no way to import modules except by filename, and import statements require valid module and package names. Modules with illegal filenames can still be used for modules that are never imported though, such as a module with only special functions like main() or extern(<non-D>) functions that are re-declared elsewhere or implicitly used by the compiler for internal runtime calls[1]. [1]: An example of the latter kind is {phobos/internal,tango/lib/compiler/*}/invariant.d which uses a keyword as module name. (It also doesn't use a module statement, and for some reason the compiler doesn't complain about this...)
Nov 10 2008
prev sibling parent "Bill Baxter" <wbaxter gmail.com> writes:
On Mon, Nov 10, 2008 at 1:59 PM, Jarrett Billingsley
<jarrett.billingsley gmail.com> wrote:
 On Sun, Nov 9, 2008 at 10:18 PM, James <james gmail.com> wrote:
 i created to include file, 1 with 'module xxx' declaration and the other
without it. but i still can import both files. what is the diff here?
Not a lot. The module declaration doesn't serve much purpose. The only things I know it's used for is a place to attach documentation for the module and as a way to make Rebuild shut up (it will whine about the file that doesn't have the module declaration at the top). Oh, and if you put an incorrect declaration on a module (say, it's foo/bar.d but you put "module bar;" instead of "module foo.bar;"), the compiler will sometimes barf. I really am not too sure what it's there for.
The compiler has no way of knowing what package you intend for the module to be in if you don't tell it with a module declaration. I guess when you don't specify a module name it just assumes a top-level module with no package. --bb
Nov 10 2008