Jump to page: 1 2
Thread overview
How to exclude function from being imported in D language?
Mar 08, 2022
BoQsc
Mar 08, 2022
Paul Backus
Mar 08, 2022
BoQsc
Mar 08, 2022
Ali Çehreli
Mar 08, 2022
bauss
Mar 09, 2022
BoQsc
Mar 09, 2022
Anonymouse
Mar 09, 2022
oculussaw
Mar 09, 2022
Ali Çehreli
Mar 18, 2022
Era Scarecrow
Mar 18, 2022
bauss
Mar 20, 2022
Salih Dincer
March 08, 2022

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?

March 08, 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:

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.

March 08, 2022

I think D Language needs and lacks conditional compilation condition and attribute of "exclude". The exclude keyword or code block in the exclude, would be made sure to not be imported by any means. Now it seems all to be only workarounds.

March 08, 2022
On 3/8/22 12:12, BoQsc wrote:

> I think D Language needs and lacks conditional compilation condition and
> attribute of "exclude".

It may be so but this need wouldn't exist if either the first program were written in a modular way to begin with or have been refactored to be so as needed (at an earlier time or as you need it now):

- main.d contains main() and the high level program logic

- foo.d contains constructs that main.d uses

The other program is similar:

- main.d contains main() etc.

- bar.d imports 'foo' from the other program

I continuously refactor my programs in that way as they get more complicated and as parts of them get used by other programs.

> The exclude keyword

I am not aware of that feature in other languages.

Ali

March 08, 2022

On Tuesday, 8 March 2022 at 20:12:40 UTC, BoQsc wrote:

>

I think D Language needs and lacks conditional compilation condition and attribute of "exclude". The exclude keyword or code block in the exclude, would be made sure to not be imported by any means. Now it seems all to be only workarounds.

What D just needs is a way to specify the entry point, in which it just defaults to the first main function found, but could be any function given.

March 09, 2022

On Tuesday, 8 March 2022 at 22:28:27 UTC, bauss wrote:

>

On Tuesday, 8 March 2022 at 20:12:40 UTC, BoQsc wrote:

>

I think D Language needs and lacks conditional compilation condition and attribute of "exclude". The exclude keyword or code block in the exclude, would be made sure to not be imported by any means. Now it seems all to be only workarounds.

What D just needs is a way to specify the entry point, in which it just defaults to the first main function found, but could be any function given.

Yeah that would be as great. Without this, the project modules feel non-interactive and always depending on other modules. Dependant modules do not feel modular, if you can't bring them to other project as a standalone module.

March 09, 2022

On Tuesday, 8 March 2022 at 22:28:27 UTC, bauss wrote:

>

On Tuesday, 8 March 2022 at 20:12:40 UTC, BoQsc wrote:

>

I think D Language needs and lacks conditional compilation condition and attribute of "exclude". The exclude keyword or code block in the exclude, would be made sure to not be imported by any means. Now it seems all to be only workarounds.

What D just needs is a way to specify the entry point, in which it just defaults to the first main function found, but could be any function given.

I just place main in a separate skeletal entrypoint.d file that contains only it (and an import to the "main" main.d/app.d/whatever), so dub can safely exclude it in unittest builds without skipping any other tests I might have in the main file.

mainSourceFile "source/myproject/entrypoint.d"
module myproject.entrypoint;

int main(string[] args)
{
    import myproject.main : run;
    return run(args);
}
March 09, 2022

I think D Language needs and lacks conditional compilation condition

March 09, 2022
On 3/9/22 05:24, oculussaw wrote:
> I think D Language needs and lacks conditional compilation condition

That statement is misleading in isolation without qualifications because D has powerful conditional compilation features.

Ali

March 18, 2022

On Tuesday, 8 March 2022 at 22:28:27 UTC, bauss wrote:

>

What D just needs is a way to specify the entry point, in which it just defaults to the first main function found, but could be any function given.

Which is similar to what Java does.

When i was first learning Java in a company i would make main() and have it run all the unittests of that particular module, then have a different file that actually combined all the tools together to run the program. Though when making the jar I'd specify which one actually was needed. But this was... 10 years ago.

« First   ‹ Prev
1 2