Thread overview
.d files without a module statement? Required to be absent?
Nov 14, 2020
WhatMeWorry
Nov 14, 2020
Paul Backus
Nov 14, 2020
H. S. Teoh
November 14, 2020
I was poking around the dmd code just to "learn from the best" and I came across some files that ended with the .d extension which did not have the module statement. (I was under the naive impression that all .d files must have a module statement)

However, in the directory:

https://github.com/dlang/dmd/blob/master/samples

I can see many examples where this is not the case. Most of them have things like Windows or C structures or calls, etc.

In stark difference, there is

https://github.com/dlang/dmd/tree/master/src/dmd/backend

where all its files seem to have file name = module name strictly enforced.

So I guess my question is when is the module statement required?  Are they recommended but not essential?  Maybe some "Best Practices" notation?

the spec sasy "Modules automatically provide a namespace scope for their contents..." so maybe my question becomes, when are namespace scopes required to be present or required to be absent?


November 14, 2020
On Saturday, 14 November 2020 at 17:55:13 UTC, WhatMeWorry wrote:
>
> I was poking around the dmd code just to "learn from the best" and I came across some files that ended with the .d extension which did not have the module statement. (I was under the naive impression that all .d files must have a module statement)

If a .d file does not have a module statement, the compiler will infer the name of the module from the path of the file. So, the file `foo/bar.d` will have its module name inferred as `foo.bar`.

There is one exception to this: the file `foo/package.d` will have its module name inferred as `foo`, not `foo.package`.
November 14, 2020
On Sat, Nov 14, 2020 at 05:55:13PM +0000, WhatMeWorry via Digitalmars-d-learn wrote:
> 
> I was poking around the dmd code just to "learn from the best"

IMNSHO, Phobos is more representative of typical D code than dmd; dmd code was automatically translated from C++, so a lot of it may still have a lot of C++-isms that wouldn't be in "native" D code.


> and I came across some files that ended with the .d extension which did not have the module statement. (I was under the naive impression that all .d files must have a module statement)

No, if there is no module declaration, the module name will be inferred from the filename.


T

-- 
"You know, maybe we don't *need* enemies." "Yeah, best friends are about all I can take." -- Calvin & Hobbes