template declareInt(x: expr) =
var x: int
declareInt(x) # error: unknown identifier: 'x'
But it succeeds for me, I tried it on 10.2 and 10.3 (devel as of today)
wink@desktop:~/prgs/nim/bmtool$ nim c -r tests/test2.nim config/nim.cfg(53, 2) Hint: added path: '/home/wink/.babel/pkgs/' [Path] config/nim.cfg(54, 2) Hint: added path: '/home/wink/.nimble/pkgs/nimble-0.6.0' [Path] config/nim.cfg(54, 2) Hint: added path: '/home/wink/.nimble/pkgs/' [Path] Hint: used config file '/opt/nim/config/nim.cfg' [Conf] Hint: used config file '/home/wink/prgs/nim/bmtool/nim.cfg' [Conf] Hint: system [Processing] Hint: test2 [Processing] [Linking] Hint: operation successful (9010 lines compiled; 0.205 sec total; 14.148MB; Debug Build) [SuccessX] /home/wink/prgs/nim/bmtool/tests/test2 x=0
Is it me, a documentation bug or compiler bug?
? the manual says it is an error but the compiler compiles it without an error (I just tested 0.9.4, 0.10.2 and current devel).
template declareInt(x: expr) =
var x: int
declareInt(x)
echo x
output
0
Here's another strange thing, below I have two immediate templates with the same name and based on the documentation I would have expected this to fail because the documentation says:
"An immediate template does not participate in overload resolution and so its arguments are not checked for semantics before invocation. So they can receive undeclared identifiers"
template t1(name: string) {.immediate.} =
echo "t1(name: string) name=", name
template t1(i: int) {.immediate.} =
echo "t1(i: int) i=", i
t1("a-name")
let i=2
t1(i)
Instead we see that t1("a-name") invoking the second template, t1(i: int), i.e. a template expecting an integer is was passed a string?
wink@desktop:~/prgs/nim/bmtool$ nim c -r tests/test2.nim
config/nim.cfg(53, 2) Hint: added path: '/home/wink/.babel/pkgs/' [Path]
config/nim.cfg(54, 2) Hint: added path: '/home/wink/.nimble/pkgs/nimble-0.6.0' [Path]
config/nim.cfg(54, 2) Hint: added path: '/home/wink/.nimble/pkgs/' [Path]
Hint: used config file '/opt/nim/config/nim.cfg' [Conf]
Hint: used config file '/home/wink/prgs/nim/bmtool/nim.cfg' [Conf]
Hint: system [Processing]
Hint: test2 [Processing]
test2.nim(1, 9) Hint: 't1' is declared but not used [XDeclaredButNotUsed]
gcc -c -w -I/opt/nim/lib -o /home/wink/prgs/nim/bmtool/tests/nimcache/test2.o /home/wink/prgs/nim/bmtool/tests/nimcache/test2.c
[Linking]
Hint: operation successful (9014 lines compiled; 0.249 sec total; 14.148MB; Debug Build) [SuccessX]
/home/wink/prgs/nim/bmtool/tests/test2
t1(i: int) i=a-name
t1(i: int) i=2
If they aren't immediate then it works like we'd expect the string using the first t1 and the integer the second:
template t1(name: string) =
echo "t1(name: string) name=", name
template t1(i: int) =
echo "t1(i: int) i=", i
t1("a-name")
let i=2
t1(i)
wink@desktop:~/prgs/nim/bmtool$ nim c -r tests/test2.nim
config/nim.cfg(53, 2) Hint: added path: '/home/wink/.babel/pkgs/' [Path]
config/nim.cfg(54, 2) Hint: added path: '/home/wink/.nimble/pkgs/nimble-0.6.0' [Path]
config/nim.cfg(54, 2) Hint: added path: '/home/wink/.nimble/pkgs/' [Path]
Hint: used config file '/opt/nim/config/nim.cfg' [Conf]
Hint: used config file '/home/wink/prgs/nim/bmtool/nim.cfg' [Conf]
Hint: system [Processing]
Hint: test2 [Processing]
gcc -c -w -I/opt/nim/lib -o /home/wink/prgs/nim/bmtool/tests/nimcache/test2.o /home/wink/prgs/nim/bmtool/tests/nimcache/test2.c
[Linking]
Hint: operation successful (9014 lines compiled; 0.251 sec total; 14.148MB; Debug Build) [SuccessX]
/home/wink/prgs/nim/bmtool/tests/test2
t1(name: string) name=a-name
t1(i: int) i=2