#Is it possible to parameterize `pub const pkgOption` during comptime?

1 messages · Page 1 of 1 (latest)

vestal matrix
#

I'm writing a package that allows users (eg, myself) to modify a const during compilation, thus altering the default behaviour of the package. For example:

main.zig

const pkg = import("pkg.zig");

pkg.max_mode = true;
_ = pkg.run(.{});

pkg.zig

pub const max_mode = false; // default

pub const PkgOptions = struct {
    max_len: usize = if (!max_mode) 20 else 0,
    // ...
}

pub fn run(opt: PkgOptions) bool {
   // just some logic that depends on PkgOptions's defaults
   return if (opt.max_len != 0) true else false;
}

In the example above, I mocked a package with a predefined set of default options stored in PkgOptions. Depending on the mode set by max_mode, the defaults options are expected to change accordingly. Do you think it is possible to achieve something like that?

#

The problem is that if I change max_mode simply to var, the compiler adequately says:

package.zig:4:26: error: unable to evaluate comptime expression
    max_len: usize = if (!max_mode) 20 else 0,
                         ^~~~~~~~~
#

Thereby I want max_mode to be comptime known but changeable once during the comptime.

fresh oxide
#

you can do the same thing std does and check options from @root

#

or give your module build time options in build.zig