www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - .d files without a module statement? Required to be absent?

reply WhatMeWorry <kheaser gmail.com> writes:
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?
Nov 14 2020
next sibling parent Paul Backus <snarwin gmail.com> writes:
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`.
Nov 14 2020
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
Nov 14 2020