really why module declarations?

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 26 16:07:48 PDT 2017


On Sunday, 26 March 2017 at 20:51:01 UTC, XavierAP wrote:
> I've perused both the spec[1] and Andrei's book, and I the idea 
> I get is that module declarations are optional, recommended 
> only in case of file names not being valid D names. But in the 
> community (and Phobos) I see it's strongly recommended and used 
> throughout.
>
> What's the reason? If the declaration overrides the path 
> (provided the file is found) rather than enforcing path 
> consistency by outputting a compile error, then what's the 
> benefit of module declarations, if we have to be disciplined to 
> keep it consistent with paths anyway?
>
> I'm busy starting my first big multi file D project, thanks for 
> any feedback!
>
>
> [1] https://dlang.org/spec/module.html#ModuleDeclaration

In addition to what Adam has said, they allow you to treat the 
module as a symbol during compilation. You _can_ do that with the 
implicitly generated one, but you can't do things that requires 
the declared symbol, e.g. attach UDAs which is how dcompute 
distinguishes device modules from host modules and possibly 
reflect of the symbols (although you can use __MODULE__ for that).


More information about the Digitalmars-d-learn mailing list