v icon indicating copy to clipboard operation
v copied to clipboard

Comptime conditional import

Open johann-koebbe-vericity opened this issue 1 year ago • 2 comments

Describe the feature

I'd like to be able to do something like

import verify

$if audit ? {
    import audit
}

fn main() {
    verify.run()
    $if audit ? {
        audit.run()
    }
}

Use Case

I'm working on a CLI application that runs in two modes, verify and audit, and they share a lot of functionality. Each mode has distinct audiences. The users of verify mode do not need audit mode, and I'd like to keep the resulting executable as small as possible by not including the audit code in it. The users who need audit mode (currently only me) would compile the source themselves with something like

$ v -d audit -prod .

I could create two separate applications, but I'd prefer to keep it as one.

Proposed Solution

Allow comptime conditional imports.

Other Information

No response

Acknowledgements

  • [ ] I may be able to implement this feature request
  • [X] This feature might incur a breaking change

Version used

V 0.4.0 4cf8328

Environment details (OS name and version, etc.)

Current developing on macOS Ventura 13.4.1, but I also develop/test on Linux (Alma 9.2) and Windows 10.

johann-koebbe-vericity avatar Aug 09 '23 16:08 johann-koebbe-vericity

I agree that it would be a good and logical feature.

The current workaround, is much more verbose, and non intuitive.

It consists of having 2 .v files: feature_notd_audit.c.v:

module main
fn audit_run(){}

feature_audit.c.v:

module main
import audit
fn audit_run(){ audit.run() }

... then inside your main, call audit_run(), which will be available in both modes, but will only do something when compiled with -d audit .

spytheman avatar Aug 13 '23 12:08 spytheman

Just a voice in support of this feature!

This is very useful when building embedded software where you have a common codebase that needs to be built using cpu architecture or platform specific constants which are best kept in a separate module which is imported conditionally by specifying compile time parameters.

I am sure there would be other use cases too.

raw-bin avatar Jun 29 '24 20:06 raw-bin