Nim macro parameters -
here' code want compile:
macro definesomething(amount:expr):stmt= var amountint = intval(amount).int # boring staff definesomething(42); it works perfectly. have want inside macro can operate staff in own way.
but think, that, better remove magic number const settings:
const magic_amount:int = 42 # whole lot of strings definesomething(magic_amount) this code fails. because magic_amount literally not integer value unlike 42 magic number.
so, how variable value inside macros in nim?
expressions untyped. since want integers can specify parameter typed , should compile:
import macros macro definesomething(amount: typed):stmt= echo gettype(amount) var amountint = intval(amount).int echo "hey ", amount_int const magic_amount = 42 definesomething(43) definesomething(magic_amount) or use normal int parameter type unless want passed other types case on parameter's kind inside macro.
Comments
Post a Comment