No -c no main()

Ary Borenszweig ary at esperanto.org.ar
Fri Mar 20 04:59:19 PDT 2009


Mike Parker escribió:
> Yigal Chripun wrote:
>> Mike Parker Wrote:
>>
>>> Yigal Chripun wrote:
>>>> On 19/03/2009 12:19, Walter Bright wrote:
>>>>> It's a good idea. Please add to bugzilla as an enhancement request!
>>>> another thing that can be done is provide a compiler flag to specify 
>>>> the module that contains main() so the compiler will use that 
>>>> specific main and ignore all others. this is similar to how you 
>>>> specify the class with main() method in Java. Jar files have a 
>>>> manifest with this info and in Eclipse you specify the class in the 
>>>> Run dialog. I don't remember how it works with the command line 
>>>> (actually, I don't remember ever using the command line with Java)
>>> It's not something that happens at compile time. All existing main 
>>> methods are compiled into their respective class files. From the 
>>> command line, you execute the JRE (java or javaw in Sun's case) and 
>>> pass it the name of the class you want to execute. If the class has a 
>>> main method, it is called. If not, you get an error.
>>
>> Java's model is complicated. it has the concept of class-loaders. What 
>> I'm suggesting is this: given three modules a,b and c all defining the 
>> main() function, you can do the following:
>> dmd a.d b.d c.d --main=b.d currently the compiler will complain about 
>> multiple definitions of main(), but with the above flag, it could skip 
>> over the main() functions in a and c.
>> the next logical step would be to allow:
>> dmd -c a.d
>> dmd -c b.d dmd -c c.d
>> dmd a.obj b.obj c.obj --main=b.obj
>>
>> in the above all three obj files have a main() method. this time it 
>> must be the *linker* that needs to understand the flag and only link 
>> the main() function into the executable. this requires changing the 
>> linker which is written in asm so less likely to happen.
>> I was not suggesting D to use a VM or the class-loader concept as in 
>> Java. even if we want that concept in D it needs to have a different 
>> implementation than in Java since there are problems with the way Java 
>> does this. (that's besides the differences due to the JVM)
> 
> I understand what you were suggesting. My point is that in Java, which 
> entry point to use is decided at run time, not compile time. From that 
> perspective, it's a useful feature. For a statically compiled language 
> like D, I don't see any obvious benefit. I mean, what is the benefit of 
> this over delegating to a specific pseudo-main method based on a 
> commandline arg or config file?

It is useful. Suppose you are writing a new module and want to test 
something quickly (not with unittests, maybe make a little program to 
see how a specific feature you just wrote works). So you write a main 
right there in the module, compile it and link it saying that the main 
is in your new module. And you can then run it.

If you couldn't do that you'd have to search for your only main in your 
project, change it by adding the necessary imports, compile it and run 
it. Way more steps.

Also, that way each module can have a main method that shows some of 
it's funcionality with a little program.



More information about the Digitalmars-d mailing list