Conpile-Time module constructor
Paul Backus
snarwin at gmail.com
Sun Jun 19 16:25:51 UTC 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 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.
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).
More information about the Digitalmars-d-learn
mailing list