I am running a macro, where I create some user-defined objects. These objects define $ and I try to display them with echo during macro execution. The problem is - when I define the $ as proc it works correctly. If I define it as method however - I get an empty string. Illustration:
import macros
type
A = ref object of RootObj
proc `$` (f: A): string=
return "Object A"
type
B = ref object of RootObj
method `$` (f: B): string {.base.}=
return "Object B"
type
C = ref object of B
method `$` (f: C): string=
return "Object C"
macro x(a: int): untyped=
echo "Compile time"
result = new_nim_node(nnk_stmt_list)
echo A()
echo B()
echo C()
x(1)
echo "Runtime"
echo A()
echo B()
echo C()
After running nim c -r dol.nim I get:
Hint: used config file '/home/zefciu/Projekty/nim/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: dol [Processing]
Hint: macros [Processing]
Compile time
Object A
Hint: [Link]
Hint: operation successful (11606 lines compiled; 0.205 sec total; 21.32MiB peekmem; Debug Build) [SuccessX]
Hint: /home/zefciu/Projekty/voovoo/dol [Exec]
Runtime
Object A
Object B
Object C
Could someone explain why is is behaving like this? Is there something, I can do to use method $ during macro execution?