Search code examples
d

Using dash character in main source file name


I tried to compile a file hello-world.d with llvm D compiler and got this message:

Error: module hello-world has non-identifier characters in filename,
use module declaration instead

I looked the documentation for what is a module declaration, and then added

module hello_world;

to the top of my file. Then the hello-world file compiled. Please explain why is that needed, what module name I am supposed to use if my main file.


Solution

  • In short - if you do not give the module a name, the D compiler will use the file-name (without extension) as module-name.

    So, if you name your D source file hello-world.d, and it does not have something like module hello_world; at the top of the source-file, the compiler will actually try to insert and compile module hello-world; and that will fail because hello-world is not a valid identifier.

    If the file name was hworld.d for an example, then the compiler will actually compile a module with auto-generated name hworld.