www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to exclude function from being imported in D language?

reply BoQsc <vaidas.boqsc gmail.com> writes:
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?
Mar 08 2022
parent reply Paul Backus <snarwin gmail.com> writes:
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
Mar 08 2022
next sibling parent reply BoQsc <vaidas.boqsc gmail.com> writes:
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.
Mar 08 2022
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Mar 08 2022
prev sibling parent reply bauss <jj_1337 live.dk> writes:
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.
Mar 08 2022
next sibling parent BoQsc <vaidas.boqsc gmail.com> writes:
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.
Mar 09 2022
prev sibling next sibling parent reply Anonymouse <zorael gmail.com> writes:
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" ``` ```d module myproject.entrypoint; int main(string[] args) { import myproject.main : run; return run(args); } ```
Mar 09 2022
parent reply oculussaw <oculussaw gmail.com> writes:
I think D Language needs and lacks conditional compilation 
condition
Mar 09 2022
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Mar 09 2022
prev sibling parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
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.
Mar 17 2022
parent bauss <jj_1337 live.dk> writes:
On Friday, 18 March 2022 at 03:24:10 UTC, Era Scarecrow wrote:
 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.
Yeah, it's similar to most other languages that allows it too. It doesn't make much sense to force an entry point anyway, especially not in D where there is no real entry point anyway, but the compiler already emits a couple of different ones that are platform dependent.
Mar 18 2022
prev sibling parent Salih Dincer <salihdb hotmail.com> writes:
On Tuesday, 8 March 2022 at 18:38:47 UTC, Paul Backus wrote:
 On Tuesday, 8 March 2022 at 17:47:47 UTC, BoQsc wrote:
 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...
 ```
There is no need for extra features when there is a version feature in D. Moreover, you can abstract the libraries it depends on, as I did [here](https://forum.dlang.org/post/dcfkmssxbzgbkkparfuv forum.dlang.org). SDB 79
Mar 19 2022