info(3 + 4)
Output:
type: int, value: 7
Hint:
- Use
typeof
on`expr`
.- Combine
echo
,astToStr
, andtype
introspection.
There's a difference between calling a function vs calling a macro. The parentheses syntax makes them feel the same, but they're not.
You can echo the expression back from the macro.
macro info(expr: untyped): untyped =
result = quote do:
echo astToStr(`expr`), " =", `expr`
echo "type: ", typeof(`expr`), ", value: ", `expr`
# Test cases
let n = 42
info(n + 1)
let word = "hi there"
info(word.len)
info(3.14 * 2)