Hi, I have a feature in my code that should not be compiled/run (it contains macros) unless the user specifically enables it using a compiler switch. If the user doesn't use the feature, it should be invisible to the user -- that is, there should be no need for the user to specify any compilation switch at all.
Currently in my code I have:
when defined(magicFeature):
include magicFeatureCode
else:
include dummyImplementation
My intention is that the user can enable the feature using the --define:magicFeature switch (as described here and here and here ).
But when the --define:magicFeature switch is NOT specified, there is a compiler error:
Warning: undeclared conditional symbol; use --symbol to declare it: magicFeature
I understand that I can declare the symbol using --symbol:magicFeature, but I would prefer that the user should NOT have to specify any extra switches if they don't know about my feature or don't want to use it.
Is it possible for me to declare the magicFeature conditional symbol in my code (with an "unset" value), so the user doesn't need to specify that switch?
(I can see that the Nim built-in symbols are declared and defined in Nim/compiler/condsyms.nim using declareSymbol and defineSymbol, but it seems like it's not possible to access this module from non-compiler code.)
Thanks for your help.
I also tried using declared instead of defined, but it seems that declared tests something else:
$ cat testdecl.nim
import macros
macro msg(s: expr): stmt =
expectKind(s, nnkStrLit)
hint($s)
result = newStmtList()
when declared(foo):
msg("foo is declared")
else:
msg("foo is NOT declared")
$ ../Nim/bin/nim c --noLinking --noMain testdecl.nim
[...]
testdecl.nim(5, 8) Hint: foo is NOT declared [User]
Hint: operation successful (11440 lines compiled; 0.146 sec total; 18.189MB) [SuccessX]
$ ../Nim/bin/nim c --noLinking --noMain --symbol:foo testdecl.nim
[...]
testdecl.nim(5, 8) Hint: foo is NOT declared [User]
Hint: operation successful (11440 lines compiled; 0.153 sec total; 18.189MB) [SuccessX]
For now, you can just disable the warning, as follows:
{.push warning[User]: off.}
when defined(foo):
echo "foo"
else:
echo "bar"
{.pop.}
@Jehan: Thanks for the code. That's a more satisfactory solution for me. I inserted the push & pop pragmas in my code, and it solved the problem for me.
In fact, I ended up pushing & popping several times, so that warnings (if any) would still be emitted about any problems in my code:
{.push warning[User]: off.}
when defined(foo):
{.pop.}
include fooImpl
{.push warning[User]: off.}
else:
{.pop.}
include dummyImpl
{.push warning[User]: off.}
{.pop.}
@Araq: Thanks also for your code fix. I confirm that re-building with the devel branch fixed the problem for me.