Nim: Parameter of macros can't be evaluate at compile-time?

Created on 1 Apr 2018  路  5Comments  路  Source: nim-lang/Nim

Hi. I've got exception:

macro init*(backend: static[int]): typed =
  when backend == 1: # excepted with message: "cannot evaluate at compile time: backend"
    discard
  else:
    discard

init 1

Parameters and result of the macros are evaluating before compilation, no?
Thanks.

All 5 comments

@r3d9u11 you should use "if" instead of "when" because macro is already executed at compile-time

Change when to if.

@Yardanico @data-man thanks for hint, but I can't use if statement.

inside when statement I'm calling another macros, who doing import of different packages (due to backend). and it can be used only with when statement.

template init*(backend: static[int]): typed =
  when backend == 1: # excepted with message: "cannot evaluate at compile time: backend"
    discard
  else:
    discard

init 1

@data-man many thanks, this works! I should use templates instead of macros in this task.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  27Comments

anthraxx picture anthraxx  路  26Comments

yglukhov picture yglukhov  路  46Comments

xland picture xland  路  26Comments

dom96 picture dom96  路  30Comments