c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Proposal: allow top-level conditional for imports

Open LowByteFox opened this issue 5 months ago • 2 comments

While it is possible to have separate modules using @if

module foo @if(...);

module foo @if(...);

I think it could be neat to also have this supported with import, so that less code duplication could occur

module foo;
import bar @if(...);

As of now, compiler returns Error: Only '@public' and '@norecurse' are valid attributes here. as expected.

LowByteFox avatar Jun 28 '25 06:06 LowByteFox

What situation would it solve? When I last tried this I couldn't find a usecase.

lerno avatar Jun 28 '25 17:06 lerno

for example having something like:

module foo @if(!$feature(XYZ));

import thirdparty;

when I have feature XYZ enabled, the imports still end up getting analyzed even tho the code is unused, and it would be kinda nice if I was able to remove thirdparty from dependencies: [...]

yes, I know it won't end up getting linked, but feels unnecessary to keep it there and store thirdparty.c3l in libs

maybe this way, it could help omit import being analyzed, I haven't looked into how the compiler performs static analysis and how would this affect further code.

LowByteFox avatar Jun 28 '25 18:06 LowByteFox

Ordering here is a bit gnarly. That's the main problem with any @if

lerno avatar Jul 01 '25 19:07 lerno

I have fixed examples like this:

module foo @if($feature(XYZ));
import thirdparty;

So that if a module is disabled, then the error on an import is ignored. This was significantly easier than adding @if to imports. I hope this is sufficient.

lerno avatar Nov 02 '25 21:11 lerno

Seems to be working fine, this should be sufficient.

LowByteFox avatar Nov 05 '25 08:11 LowByteFox