It depends on what you are trying to do. Program D requires only one main function for all modules as an entry point, so there is no implicit way, as in Python. Path D is to create the executable as a separate module that contains main and imports another module.
But if you just want to do this for testing, you have to put the executable code in unittest blocks (without main ), and then you can run the file with rdmd -main -unittest scratch.d , which will add stub main for you.
If you really want to make a module with a dual purpose (which is not a D Way), you can put main in a unique version block:
module scratch; // file scratch.d import std.stdio; void foo(){ writeln("FOO"); } version(scratchExe) { void main() { foo(); } }
Then compile the executable version with dmd scratch.d -version=scratchExe .
source share