Hey guys. I've encountered a problem when using strformat which I can't figure out. If I have a template that accepts a string and I use that string twice in the template body, I get a C compiler error, but only if the string is generated using the fmt"" macro. Here's a minimal example:
import strformat
template myTemplate(s: string) =
echo s
echo s
proc main() =
myTemplate fmt"hello"
main()
If I try to compile this, I get the following error message
/home/XXX/.cache/nim/template_test_d/@mtemplate_test.nim.c: In function ‘main__template95test_2’:
/home/XXX/.cache/nim/template_test_d/@mtemplate_test.nim.c:118:24: error: redeclaration of ‘fmtRes’ with no linkage
118 | NimStringDesc* fmtRes;
| ^~~~~~
/home/XXX/.cache/nim/template_test_d/@mtemplate_test.nim.c:116:24: note: previous declaration of ‘fmtRes’ with type ‘NimStringDesc *’
116 | NimStringDesc* fmtRes;
| ^~~~~~
If I don't use the strformat macro, this compiles as expected, and if I remove the the main() function and just call myTemplate globally (is that what you call it?) it also compiles. Does anyone know what the issue might be?
Ok, will do.
After doing some further testing I discovered that you can at least work around this issue by using a temporary variable:
import strformat
template myTemplate(s: string) =
let s2 = s
echo s2
echo s2
proc main() =
myTemplate fmt"hello"
main()