www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why should file names intended for executables be valid identifiers?

reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
I understand that module names need to be valid identifiers in that other 
modules would need to import them. But when a file is intended to be just an 
executable, why is it mandatory to give it a module declaration with a valid 
identifier? 

For instance, hyphens are often used as part of executable names on Linux, 
but if I do this:

$ dmd usage-printer.d

I get the following error:

usage-printer.d: Error: module usage-printer has non-identifier characters 
in filename, use module declaration instead

I expect it should not be difficult for the compiler to see that this D file 
is not a module being imported by anything else or even being compiled to a 
library which would need to be later imported. In which case, why does it 
insist that the file should be given a valid module name?

-- 

Dec 14 2015
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 15 December 2015 at 03:31:18 UTC, Shriramana Sharma 
wrote:
 I understand that module names need to be valid identifiers in 
 that other modules would need to import them. But when a file 
 is intended to be just an executable, why is it mandatory to 
 give it a module declaration with a valid identifier?
It still has a module name that can be used in reflection, must be used in name disambiguation (at the linker level if nothing else, any functions are mangled with the module name so they don't conflict with C functions with the same name), and other things. Just use the module declaration, you can always call it something like `module main;` generically if you know there is just one in your file.
Dec 14 2015
parent reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
Adam D.  Ruppe wrote:

 It still has a module name that can be used in reflection, must
 be used in name disambiguation (at the linker level if nothing
 else, any functions are mangled with the module name so they
 don't conflict with C functions with the same name), and other
 things.
Sorry but I don't get this fully: can't a hyphen be part of such mangled names? Aren't they just strings that the linker hashes or something? (My knowledge of compiler/executable internals is limited.) And any reflection of the module name would also be just a string which need not be a valid identifier no? --
Dec 16 2015
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2015-12-17 05:26, Shriramana Sharma wrote:

 Sorry but I don't get this fully: can't a hyphen be part of such mangled
 names? Aren't they just strings that the linker hashes or something? (My
 knowledge of compiler/executable internals is limited.)
I'm not sure about a hyphen but a dollar sign is allowed for example.
 And any reflection
 of the module name would also be just a string which need not be a valid
 identifier no?
I would guess it's to simplify the compiler. Funny thing: Ruby allows you to have names of methods that you cannot declare and cannot call :). The trick is to use metaprogramming and reflection to declare and call the methods. -- /Jacob Carlborg
Dec 16 2015
prev sibling next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 17 December 2015 at 04:26:04 UTC, Shriramana Sharma 
wrote:
 Sorry but I don't get this fully: can't a hyphen be part of 
 such mangled names?
I'm actually not sure but I have never seen it done.
 And any reflection of the module name would also be just a 
 string which need not be a valid identifier no?
Reflection would be a string, but it is often mixed in and the other uses like name disambiguation is still identifier.
Dec 17 2015
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/16/2015 08:26 PM, Shriramana Sharma wrote:

 can't a hyphen be part of such mangled names?
Perhaps my response is naive but hyphen means subtraction (or minus). If the grammar is context-free then it cannot appear in a name, no? Ali
Dec 17 2015
prev sibling next sibling parent Jon D <jond noreply.com> writes:
On Tuesday, 15 December 2015 at 03:31:18 UTC, Shriramana Sharma 
wrote:
 For instance, hyphens are often used as part of executable 
 names on Linux, but if I do this:

 $ dmd usage-printer.d

 I get the following error:

 usage-printer.d: Error: module usage-printer has non-identifier 
 characters in filename, use module declaration instead
Try adding the line: module usage_printer; at the top of the file. This overrides the default module name (same as file name). --Jon
Dec 14 2015
prev sibling parent Alex Parrill <initrd.gz gmail.com> writes:
On Tuesday, 15 December 2015 at 03:31:18 UTC, Shriramana Sharma 
wrote:
 I expect it should not be difficult for the compiler to see 
 that this D file is not a module being imported by anything 
 else or even being compiled to a library which would need to be 
 later imported. In which case, why does it insist that the file 
 should be given a valid module name?
All files in D are modules. The only thing special about the "main" module is that it exports a function called `main`, which druntime calls at startup. It's like asking why you need to have a class in Java that only contains `public static void main(...)`, instead of just having the main function itself; it's simply how D is designed, and it reduces the number of special cases. Though after compiling, module names are irrelevant; you can name the binary whatever you want: `dmd -o whatever-filename.exe my_module.d` Name mangling is irrelevant; you can't have a dash in the module name because it would make certain code ambiguous (ex. `foo.my-module.bar`: is one module field reference or a subtraction between two field references?)
Dec 17 2015