How to exclude function from being imported in D language?

Paul Backus snarwin at gmail.com
Tue Mar 8 18:38:47 UTC 2022


On Tuesday, 8 March 2022 at 17:47:47 UTC, BoQsc wrote:
> Premise: In D language, only one main(){} function can exist in 
> a program.
> Having two `main()` functions throws an error.
>
> Let's say I want to use some functionality of another program, 
> but it has a `main(){}`
> function. How can I import and use functions without importing 
> the `main(){}` function?

You'll have to modify the other program to exclude the `main` 
function from compilation.

For example, you could use a [`version` condition][1]:

```d
module otherprogram;

version (Otherprogram_NoMain)
{
     // no main function
}
else
{
     void main(string[] args)
     {
         // ...
     }
}

// other functions...
```

Then, when you're compiling the program that uses it, you can 
pass `-version=Otherprogram_NoMain` on the command line:

```
$ dmd -version=Otherprogram_NoMain myprogram.d otherprogram.d
```

This will include the `main` function from `myprogram.d`, but 
exclude the one from `otherprogram.d`.

[1]: https://dlang.org/spec/version.html#version


More information about the Digitalmars-d-learn mailing list