digitalmars.D.learn - Conpile-Time module constructor
- Ruby The Roobster (2/2) Jun 19 2022 Is it possible to make a module constructor run at compile-time?
- Paul Backus (5/7) Jun 19 2022 No, it's not.
- Ruby The Roobster (4/13) Jun 19 2022 What I was trying to accomplish was to change a compile time array
- Paul Backus (12/26) Jun 19 2022 You can do this with a `static if` check:
- Ruby The Roobster (4/27) Jun 19 2022 Curious - how exactly does this determine if the module is included? I
- Paul Backus (10/22) Jun 19 2022 You're right--technically, it doesn't check whether the module is
Is it possible to make a module constructor run at compile-time? If so, how?
Jun 19 2022
On Sunday, 19 June 2022 at 14:51:26 UTC, Ruby The Roobster wrote:Is it possible to make a module constructor run at compile-time? If so, how?No, it's not. What are you trying to accomplish that lead you to ask this question? There is probably a different way to do it without involving module constructors.
Jun 19 2022
On 6/19/2022 11:19 AM, Paul Backus wrote:On Sunday, 19 June 2022 at 14:51:26 UTC, Ruby The Roobster wrote:What I was trying to accomplish was to change a compile time array whenever a certain module was included. The value of the array needs to be known at compile time.Is it possible to make a module constructor run at compile-time? If so, how?No, it's not. What are you trying to accomplish that lead you to ask this question? There is probably a different way to do it without involving module constructors.
Jun 19 2022
On Sunday, 19 June 2022 at 15:34:48 UTC, Ruby The Roobster wrote:On 6/19/2022 11:19 AM, Paul Backus wrote:You can do this with a `static if` check: static if (__traits(compiles, { import mymodule; })) { // mymodule is included enum compileTimeArray = ...; } else { // mymodule is not included enum compileTimeArray = ...; }On Sunday, 19 June 2022 at 14:51:26 UTC, Ruby The Roobster wrote:What I was trying to accomplish was to change a compile time array whenever a certain module was included. The value of the array needs to be known at compile time.Is it possible to make a module constructor run at compile-time? If so, how?No, it's not. What are you trying to accomplish that lead you to ask this question? There is probably a different way to do it without involving module constructors.
Jun 19 2022
On 6/19/2022 11:55 AM, Paul Backus wrote:On Sunday, 19 June 2022 at 15:34:48 UTC, Ruby The Roobster wrote:Curious - how exactly does this determine if the module is included? I thought __traits(compiles, ...) only checks if an expression is SEMANTICALLY correct.On 6/19/2022 11:19 AM, Paul Backus wrote:You can do this with a `static if` check: static if (__traits(compiles, { import mymodule; })) { // mymodule is included enum compileTimeArray = ...; }On Sunday, 19 June 2022 at 14:51:26 UTC, Ruby The Roobster wrote:What I was trying to accomplish was to change a compile time array whenever a certain module was included. The value of the array needs to be known at compile time.Is it possible to make a module constructor run at compile-time? If so, how?No, it's not. What are you trying to accomplish that lead you to ask this question? There is probably a different way to do it without involving module constructors.
Jun 19 2022
On Sunday, 19 June 2022 at 15:57:41 UTC, Ruby The Roobster wrote:On 6/19/2022 11:55 AM, Paul Backus wrote:You're right--technically, it doesn't check whether the module is *included* in the final build, only whether the module is *available* for inclusion at compile time. Because D supports separate compilation of modules, there is no way to check at compile time what modules will be included in the final build. If that's what you want to test for, you will have to move the check out of the code itself and into your build system (for example, by passing a `-version=HasMyModule` flag for builds that include the module in question).You can do this with a `static if` check: static if (__traits(compiles, { import mymodule; })) { // mymodule is included enum compileTimeArray = ...; }Curious - how exactly does this determine if the module is included? I thought __traits(compiles, ...) only checks if an expression is SEMANTICALLY correct.
Jun 19 2022