type app_configuration = tuple
something1 : int
something2 : string
do_logging : static[bool] = false
static_something1 : static[int] = 100
static_something2 : static[bool] = true
static_something3 : static[int] = compile_time_func()
proc foo(cfg : ptr app_configuration, ...) =
when cfg.do_logging == true: log(...)
...
bar(cfg, ...)
Why dont you use templates for that:
template do_logging*(ac: app_configuration): bool = false
Btw, didn't know you could declare tuples with that layout!
Hi @PV, I don't think that this code could ever work:
proc foo(cfg : ptr app_configuration, ...) =
when cfg.do_logging == true: log(...)
A when statement is evaluated at compile time, and it must take a constant argument/expression, but cfg is passed into proc foo when proc foo is invoked. Proc foo must be "compiled" (parsed / checked semantically / interpreted) before it can be invoked, and by that stage the when should already have been evaluated.
If you want to parametrize the program at compile time, why not use compile-time proc 'defined'? Then you can use -d:x or --define:x compiler switch for conditional compilation.
I use this technique in Pymod so that module pymod is inert unless compile-time symbol "pmgen" is defined.
@jboy: The point is that cfg.do_logging would be always constant and known during compilation, regardless of cfg. Having parameters inside a structure is scalable. Imagine dozen or two options passed through command line.
It should be also possible to use defined() to detect if some option is missing in the structure and act accordingly.
@PC you cannot store a static member inside a struct, but you can use generics and store that as a generic parameter. I do this to store matrix and vector dimensions in my linear algebra library, whenever they are known at compile time. This has also the advantage that these static values do not take any space at runtime.
If you have many such values, it can become cumbersome. In that case, I would define a type T that holds all the static information, and use a single `` static[T]`` as a generic parameter