[https://github.com/nim-lang/Nim/wiki/Nim-for-Python-Programmers#Templates](https://github.com/nim-lang/Nim/wiki/Nim-for-Python-Programmers#Templates)
Far from perfect, but better than nothing, Templates explanation.
use bind
import strutils
template test_template*() =
bind formatFloat
echo (1.0/3.0).formatFloat(precision=3)
proc test_proc*() =
echo (1.0/3.0).formatFloat(precision=3)
Run
it works if you change the template:
template test_template*() =
echo formatFloat((1.0/3.0), precision=3)
Run
See
Your code works @treeform as long as you fix the typo in tempalte
proc b() =
echo "hi"
template a*() =
b()
Run
Inside template a, b is a sym,closedSymChoice or openSymChoice so it works
regardless of whether or not b is private or not.
Hmm, I think it always worked like this. Template just substitute code. If the
code where you have template can't access some thing... then it's an error.
It also is an error if template calls a private proc:
proc b() = # private b
echo "hi"
tempalte a*() = # public
It works if you define it like this:
# test.nim
import strutils
template test_template*() =
echo formatFloat(1.0/3.0,precision=3)
#echo (1.0/3.0).formatFloat(precision=3)
proc test_proc*() =
echo (1.0/3.0).formatFloat(precision=3)
Hello folks,
Does anyone know why templates don't "auto-import" symbols defined in their
residence file? :( Minimal example:
# test.nim
import strutils
template test_template*() =
echo (1.0/3.0).formatFloat(precision=3)
proc test_proc*() =
echo