Is it possible to execute a function inside .d script that has no main function?

Alex sascha.orlov at gmail.com
Thu Jul 11 10:41:38 UTC 2019


On Thursday, 11 July 2019 at 09:43:55 UTC, BoQsc wrote:
> Here I have a file named: module.d
>
>>import std.stdio   : writeln;
>>
>>void interestingFunction(){
>>	writeln("Testing");
>>}
>
> There is no main() function since, I want to import this 
> module, into another .d file.
>
> ( If I try to import and module.d does have main() function I 
> get this error: )
>> otherFile.d(13): Error: only one main, WinMain, or DllMain 
>> allowed. Previously found main at module.d(3)
>
>
>
>
>
> I would like to launch function "interestingFunction()" 
> directly using rdmd.
> Is it possible to achieve that by any way?
>
>
>
>
>
> I tried to launch the whole file, but it gave me some weird 
> output:
>>C:\Users\User\Desktop\Environment variables>rdmd module.d
>>OPTLINK (R) for Win32  Release 8.00.17
>>Copyright (C) Digital Mars 1989-2013  All rights reserved.
>>http://www.digitalmars.com/ctg/optlink.html
>>OPTLINK : Warning 134: No Start Address

If you are trying to test a function separately from other 
functions, e.g. for testing, then you are approaching unit 
testing.
You could write a unit test block just under the function to test 
like
unittest
{
   interestingFunction;
}

and launch it via rdmd... don't know the syntax right now. But 
here is something:
https://stackoverflow.com/questions/10694994/rdmd-is-not-running-unit-tests

So, I assume something like
rdmd -unittest --main module.d


More information about the Digitalmars-d-learn mailing list