digitalmars.D.learn - How to exclude function from being imported in D language?
- BoQsc (7/7) Mar 08 2022 Premise: In D language, only one main(){} function can exist in a
- Paul Backus (27/34) Mar 08 2022 You'll have to modify the other program to exclude the `main`
- BoQsc (4/4) Mar 08 2022 I think D Language needs and lacks conditional compilation
- =?UTF-8?Q?Ali_=c3=87ehreli?= (13/16) Mar 08 2022 It may be so but this need wouldn't exist if either the first program
- bauss (4/8) Mar 08 2022 What D just needs is a way to specify the entry point, in which
- BoQsc (5/13) Mar 09 2022 Yeah that would be as great. Without this, the project modules
- Anonymouse (17/25) Mar 09 2022 I just place `main` in a separate skeletal `entrypoint.d` file
- oculussaw (2/2) Mar 09 2022 I think D Language needs and lacks conditional compilation
- =?UTF-8?Q?Ali_=c3=87ehreli?= (4/5) Mar 09 2022 That statement is misleading in isolation without qualifications because...
- Era Scarecrow (8/11) Mar 17 2022 Which is similar to what Java does.
- bauss (6/17) Mar 18 2022 Yeah, it's similar to most other languages that allows it too.
- Salih Dincer (6/23) Mar 19 2022 There is no need for extra features when there is a version
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
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
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
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 keywordI am not aware of that feature in other languages. Ali
Mar 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.
Mar 08 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: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.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 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 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); } ```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 09 2022
I think D Language needs and lacks conditional compilation condition
Mar 09 2022
On 3/9/22 05:24, oculussaw wrote:I think D Language needs and lacks conditional compilation conditionThat statement is misleading in isolation without qualifications because D has powerful conditional compilation features. Ali
Mar 09 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.
Mar 17 2022
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: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.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 18 2022
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